硬币称重

/*问题描述:

设有8枚硬币a,b,c,d,e,f,g,h,其中有一枚硬币是伪造的。
 真伪硬币的区别仅是重量不同,可能重,可能轻。今要求以天平为工具,用最少的
 比较次数挑出伪造硬币,并鉴定它是重还是轻。

*/ 

//只是找出不标准的球,没有判别轻重,3次称量

  1. #include<iostream>
  2. using namespace std;
  3. int main(){
  4.  int a,b,c,d,e,f,g,h;
  5.  int count=0;//称量的次数
  6.  cout<<"Please Enter the eight coins weight!"<<endl;
  7.  cin>>a>>b>>c>>d>>e>>f>>g>>h;
  8.  //进行称重判断
  9.  if(a+b==c+d){//a,b,c,d都合格
  10.   count++;
  11.   if(a+b==e+f){//e,f也合格,则可推出g,h中必有一个不合格
  12.    count++;
  13.    if(a==g){//g合格,则h不合格,输出
  14.     count++;
  15.     cout<<"h is not check out!"<<endl;
  16.    }
  17.    else{
  18.     count++;
  19.     cout<<"g is not check out!"<<endl;
  20.    }
  21.   }
  22.   else{//e,f中有一个不合格
  23.    count++;
  24.    if(a==e){
  25.     count++;
  26.     cout<<"f is not check out!"<<endl;
  27.    }
  28.    else{
  29.     count++;
  30.     cout<<"e is not check out!"<<endl;
  31.    }
  32.   }//end_else
  33.  }//end_if
  34.  else{//a,b,c,d中有不合格的,则可推出e,f,g,h都合格
  35.   count++;
  36.   if(a+b==e+f){//a,b合格,则c,d中有一个不合格
  37.    count++;
  38.    if(c==e){
  39.     count++;
  40.     cout<<"d is not check out!"<<endl;
  41.    }
  42.    else{
  43.     count++;
  44.     cout<<"c is not check out!"<<endl;
  45.    }
  46.   }
  47.   else{//a,b中有不合格的
  48.    if(a==e){
  49.     count++;
  50.     cout<<"b is not check out!"<<endl;
  51.    }
  52.    else{
  53.     count++;
  54.     cout<<"a is not check out!"<<endl;
  55.    }
  56.   }
  57.  }
  58.  cout<<count<<endl;
  59.  return 0;
  60. }

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值