Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1477849
  • 博文数量: 187
  • 博客积分: 10375
  • 博客等级: 上将
  • 技术积分: 3127
  • 用 户 组: 普通用户
  • 注册时间: 2006-03-07 10:58
文章分类

全部博文(187)

文章存档

2013年(1)

2012年(8)

2011年(28)

2010年(36)

2009年(47)

2008年(67)

我的朋友

分类: Java

2009-01-01 13:49:50

12个球中选坏球: 有12个球,其中有一个是坏球,坏球的重量比其它球或轻或重,但不知道是轻还是重。
有一架天平,只准称三次,找出坏球,并说出坏球比好球是轻还是重。
 
 

package yzg.arithmetic;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * 12个球中选坏球: 有12个球,其中有一个是坏球,坏球的重量比其它球或轻或重,但不知道是轻还是重。
 * 有一架天平,只准称三次,找出坏球,并说出比好球是轻还是重。
 *
 * @author yzg
 *
 */

public class SelectBadBall {
    int s; // 键盘输入的值
    int t1; // 第一次称的结果
    int t2; // 第二次称的结果
    int t3; // 第三次称的结果
    int h; // 坏球号码
    int p; // 坏球是重还是轻,1:轻 0:重
    int x1, x2, x3, x4; // 
    int r; // 

    String[] q = { "左重", "平衡", "右重" };

    /**
     * @param args
     */

    public static void main(String[] args) {
        SelectBadBall sbb = new SelectBadBall();
        sbb.execute();
    }

    public void execute() {
        System.out.println("计算机判断12只球中的坏球问题");
        System.out.println("请你认定坏球的球号(1~12之一),并想好它是轻的或是重的!");
        System.out.println("称三次,请你回答三个问题(天平状态)。");
        System.out.println("则计算机可以推算出你认定的坏球号和轻重!");
        System.out.println("开始称球......");
        System.out.println("第一次称:1~4号球放在天平左端,5~8号球放在天平右端。");

        t1 = p3();
        p = t1;
        if (t1 == 1) {
            System.out.println("说明1~8号球是好的,坏球在9~12号球当中。");
            System.out.println("第二次称:9、10号球放在天平左端;11、1号(显然是好球)放在右端。");
            x1 = 9;
            x2 = 10;
            x3 = 11;
            x4 = 12;
            r = 1;

            t2 = p3();
            if (t2 == 1) {
                System.out.println("第三次称:" + x4 + "号球放左端," + r + "号球放右端。");
                t3 = p2();
                h = x4;
                if (t3 == 0) {
                    p = 1;
                } else {
                    p = 0;
                }
            } else {
                w3(x1, x2, x3);
            }
        } else {
            System.out.println("可推出9~12号球是好的,坏球在1~8号当中。");
            System.out.println("第二次称:左端1、2号球不动,拿走3号球,4号球与右端5号球交换,右端6号球不动,");
            System.out.println("拿走7、8号球,补放12号球(好球)。");

            t2 = p3();
            r = 12;
            if (t1 + t2 == 2) {
                System.out.println("第三次称:4号球放在天平左端,12号球放在右端。");
                t3 = p3();
                h = 4;
                if (t3 == 0) {
                    p = 1;
                } else if (t3 == 2) {
                    h = 5;
                    if (t2 == 0) {
                        p = 1;
                    } else {
                        p = 0;
                    }
                }
            } else {
                if (t1 == 0 && t2 == 0 || t1 == 2 && t2 == 2) {
                    x1 = 1;
                    x2 = 2;
                    x3 = 6;
                } else {
                    x1 = 7;
                    x2 = 8;
                    x3 = 3;
                    if (t1 == 0) {
                        t2 = 2;
                    } else {
                        t2 = 0;
                    }
                }
                w3(x1, x2, x3);
            }
        }
        System.out.println("最后推出坏球是" + h + "号,");
        if (p == 0) {
            System.out.println("是轻的!!");
        } else {
            System.out.println("是重的!!");
        }
    }

    /**
     * 两种答案选择函数(一坏球一好球)
     */

    public int p2() {
        System.out.println("请回答这时天平的状态(0:左重 2:右重):");
        s = getCh();
        if (s == 0 || s == 2) {
            System.out.println("你选择了:" + q[s]);
        } else {
            System.out.println("你选择错误!请重新选择!");
            s = p2();
        }
        return s;
    }

    /**
     * 三种答案选择函数
     *
     * @return
     */

    public int p3() {
        System.out.println("请回答这时天平的状态(0:左重 1:平衡 2:右重):");
        s = getCh();
        if (s == 0 || s == 1 || s == 2) {
            System.out.println("你选择了:" + q[s]);
        } else {
            System.out.println("你选择错误!请重新选择!");
            s = p3();
        }
        return s;
    }

    /**
     * 称三球后,称两球判断函数。
     *
     * @param x1
     * @param x2
     * @param x3
     */

    public void w3(int x1, int x2, int x3) {
        System.out.println("可推出坏球在" + x1 + "号、" + x2 + "号和" + x3 + "号之中。");
        System.out.println("第三次称:" + x1 + "放在天平左端," + x2 + "号球放在右端。");
        t3 = p3();
        if (t3 == 1) {
            h = x3;

            if (t2 == 0) {
                p = 0;
            } else {
                p = 1;
            }
        } else if (t2 == 0 && t3 == 0) {
            h = x1;
            p = 1;
        } else if (t2 == 0 && t3 == 2) {
            h = x2;
            p = 1;
        } else if (t2 == 2 && t3 == 0) {
            h = x2;
            p = 0;
        } else {
            h = x1;
            p = 0;
        }
    }

    /**
     * 接收键盘的输入
     *
     * @return 输入的数字
     */

    public int getCh() {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int ret = -1;
        try {
            ret = Integer.parseInt(br.readLine());
        } catch (NumberFormatException e) {
            // e.printStackTrace();
        } catch (IOException e) {
            // e.printStackTrace();
        }
        return ret;
    }
}

阅读(1176) | 评论(0) | 转发(0) |
0

上一篇:C语言写的模仿弹琴程序

下一篇:除夕

给主人留下些什么吧!~~