瓶子质量问题的Java代码实现

原创
2014/10/26 21:31
阅读数 173

有十二个长得一样的瓶,其中一个瓶的重量与其他瓶不同。 

用一个天平,称三次,区分出那个瓶,并说明其比其他瓶重还是轻。 



+++++++++++++++++++++++++++++++


首先将瓶编号1、2、3、4、5、6、7、8、9、10、11、12共12个瓶~~~~


一秤:

(1、2、3、4)Vs.(5、6、7、8)

×××××××××××

×××××××××××

一秤平则说明1、2、3、4、5、6、7、8瓶都是正常的。。。。。。也就是问题瓶在9、10、11、12中。


二秤:

(1、9)Vs.(10、11)

二秤平这说明9、10、11瓶都是正常的。。。。

三秤:(1)Vs.(12)得到12轻或重


二秤不平说明问题瓶在9、10、11中。。。。。

三秤:

(10)Vs.(11)

平则能判断9瓶轻重

不平且天平倾斜方向与二秤方向比不变。。则11是问题瓶兵并可判断轻重。。。不平且天平倾斜方向与二秤方向比改变。。则10是问题瓶兵可判断轻重。


×××××××××××

×××××××××××


一秤不平则说明问题瓶在1、2、3、4、5、6、7、8中。


二秤:

(1、6、9)Vs.(5、2、3)

二秤平则说明问题瓶在4、7、8中。。。。。

三秤:

(7)Vs.(8)

三秤平则能判断4瓶的轻重

不平且天平倾斜方向与一秤方向比不变。。则7是问题瓶兵并可判断轻重。。。平且天平倾斜方向与一秤方向比改变。。则8是问题瓶兵可判断轻重。


二秤不平且天平倾斜方向与二秤方向比不变。。则说明问题瓶在1、5中。

三秤:

(1)Vs.(9)

三秤平则能判断5瓶的轻重,三秤不平则能判断1的轻重


二秤不平且天平倾斜方向与二秤方向比改变。。则说明问题瓶在2、3、6中。

三秤:

(2)Vs.(3)

三秤平则能判断6瓶的轻重,三秤不平且倾斜方向与二秤方向比不变说明3瓶是问题瓶并可判断轻重,三秤不平且倾斜方向与二秤方向比改变说明2瓶是问题瓶并可判断轻重。

class Pingzi{
    private int weight;
    private boolean flag;//好坏标志
    public Pingzi(){
        this.weight=100;
        this.flag=true;
    }
    public int getWeight(){return this.weight;}
    public boolean getFlag(){return this.flag;}
    public void setWeight(int w){this.weight=w;}
    public void setFlag(boolean f){this.flag=f;}

}
public class PingziTest{
    public static void main(String[] args){
        int badFlag;
        Pingzi [] pingzi=new Pingzi[12];
        Pingzi total_pingzi_1=new Pingzi();
        Pingzi total_pingzi_2=new Pingzi();
        for(int i=0;i<12;i++){
            pingzi[i]=new Pingzi();
        }
        badFlag=(int)(12*Math.random()); 
        pingzi[badFlag].setWeight((int)(200*Math.random()));
        System.out.println("瓶子的重量分别为:");
        for(int i=0;i<12;i++){
            System.out.print(pingzi[i].getWeight()+"  ");
        }
        System.out.println();
        total_pingzi_1.setWeight(pingzi[0].getWeight()+pingzi[1].getWeight()+pingzi[2].getWeight()+pingzi[3].getWeight());
        total_pingzi_2.setWeight(pingzi[4].getWeight()+pingzi[5].getWeight()+pingzi[6].getWeight()+pingzi[7].getWeight());
        if(total_pingzi_1.getWeight()==total_pingzi_2.getWeight()){ 
            Pingzi total_pingzi_1_1=new Pingzi();
            Pingzi total_pingzi_1_2=new Pingzi();
            total_pingzi_1_1.setWeight(pingzi[0].getWeight()+pingzi[8].getWeight());
            total_pingzi_1_2.setWeight(pingzi[9].getWeight()+pingzi[10].getWeight());
            if (total_pingzi_1_1.getWeight()==total_pingzi_1_2.getWeight()) {  
                pingzi[11].setFlag(false);
            }
            else if(total_pingzi_1_1.getWeight()>total_pingzi_1_2.getWeight()){  
                if(pingzi[9].getWeight()==pingzi[10].getWeight()){
                    pingzi[8].setFlag(false);
                }else if (pingzi[9].getWeight()>pingzi[10].getWeight()) {
                    pingzi[10].setFlag(false);
                }else if (pingzi[9].getWeight()<pingzi[10].getWeight()) {
                    pingzi[9].setFlag(false);
                }
            }else if(total_pingzi_1_1.getWeight()<total_pingzi_1_2.getWeight()){  
                if (pingzi[9].getWeight()==pingzi[10].getWeight()) {
                    pingzi[8].setFlag(false);
                }else if (pingzi[9].getWeight()>pingzi[10].getWeight()) {
                    pingzi[9].setFlag(false);
                }else if (pingzi[9].getWeight()<pingzi[10].getWeight()) {
                    pingzi[10].setFlag(false);
                }
            }
        }
        else if(total_pingzi_1.getWeight()>total_pingzi_2.getWeight()){  
            Pingzi total_pingzi_2_1=new Pingzi();
            Pingzi total_pingzi_2_2=new Pingzi();
            total_pingzi_2_1.setWeight(pingzi[0].getWeight()+pingzi[5].getWeight()+pingzi[8].getWeight());
            total_pingzi_2_2.setWeight(pingzi[4].getWeight()+pingzi[1].getWeight()+pingzi[2].getWeight());
            if(total_pingzi_2_1.getWeight()==total_pingzi_2_2.getWeight()){   
                if (pingzi[6].getWeight()==pingzi[7].getWeight()) {
                    pingzi[3].setFlag(false);
                }else if (pingzi[6].getWeight()>pingzi[7].getWeight()) {
                    pingzi[7].setFlag(false);
                }else if (pingzi[6].getWeight()<pingzi[7].getWeight()) {
                    pingzi[6].setFlag(false);
                }
            }else if(total_pingzi_2_1.getWeight()>total_pingzi_2_2.getWeight()){  
                if (pingzi[0].getWeight()==pingzi[8].getWeight()) {
                    pingzi[4].setFlag(false);
                }else{
                    pingzi[0].setFlag(false);
                }
            }else if(total_pingzi_2_1.getWeight()<total_pingzi_2_2.getWeight()){
                if (pingzi[1].getWeight()==pingzi[2].getWeight()) {
                    pingzi[5].setFlag(false);
                }else if (pingzi[1].getWeight()>pingzi[2].getWeight()) {
                    pingzi[1].setFlag(false);
                }else if (pingzi[1].getWeight()<pingzi[2].getWeight()) {
                    pingzi[2].setFlag(false);
                }
            }
        }
        else if(total_pingzi_1.getWeight()<total_pingzi_2.getWeight()){   
            Pingzi total_pingzi_2_1=new Pingzi();
            Pingzi total_pingzi_2_2=new Pingzi();
            total_pingzi_2_1.setWeight(pingzi[0].getWeight()+pingzi[5].getWeight()+pingzi[8].getWeight());
            total_pingzi_2_2.setWeight(pingzi[4].getWeight()+pingzi[1].getWeight()+pingzi[2].getWeight());
            if(total_pingzi_2_1.getWeight()==total_pingzi_2_2.getWeight()){
                if (pingzi[6].getWeight()==pingzi[7].getWeight()) {
                    pingzi[3].setFlag(false);
                }else if (pingzi[6].getWeight()>pingzi[7].getWeight()) {
                    pingzi[6].setFlag(false);
                }else if (pingzi[6].getWeight()<pingzi[7].getWeight()) {
                    pingzi[7].setFlag(false);
                }
            }else if(total_pingzi_2_1.getWeight()<total_pingzi_2_2.getWeight()){ 
                if (pingzi[0].getWeight()==pingzi[8].getWeight()) {
                    pingzi[4].setFlag(false);
                }else{
                    pingzi[0].setFlag(false);
                }
            }else if(total_pingzi_2_1.getWeight()>total_pingzi_2_2.getWeight()){
                if (pingzi[1].getWeight()==pingzi[2].getWeight()) {
                    pingzi[5].setFlag(false);
                }else if (pingzi[1].getWeight()<pingzi[2].getWeight()) {
                    pingzi[1].setFlag(false);
                }else if (pingzi[1].getWeight()>pingzi[2].getWeight()) {
                    pingzi[2].setFlag(false);
                }
            }
        }
        for (int i=0;i<12 ;i++ ) {
            if (pingzi[i].getFlag()==false) {
                System.out.println("坏瓶是第"+(i+1)+"号"+" 坏瓶的质量为:"+pingzi[i].getWeight());
            }
        }
    }
}


展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部