假币之谜
假幣之謎
有八個硬幣a,b,c,d,e,f,g,h,其中一枚是假幣,從外觀無法分辨,比真幣或輕或重。使用天平,用最少比較次數(shù),找出假幣,并分辨出輕重。
#include <iostream> using namespace std; int weight(float x, float y) {if (x == y)return 0;else if (x > y)return 1;else return 2; }int main(){float a, b, c, d, e, f, g, h;cin >> a >> b >> c >> d >> e >> f >> g >> h;switch (weight(a + b + c, d + e + f)) {case 0: //a,b,c,d,e,f都是真幣switch (weight(a, g)) {case 0: //g是真幣if (weight(a, h) == 1)cout << "h是假幣,且重量輕";else cout << "h是假幣,且重量重";break;case 1: //g是假幣cout << "g是假幣,且重量輕";break;default:cout<< "g是假幣,且重量重";}break;case 1: //a,b,c與d,e,f中有假幣,如果假幣重,則在a,b,c中,如果假幣輕,則在d,e,f中switch (weight(a+e+f, d+g+h)) { //a,e,f一組,d,g,h一組case 0: //b,c是假幣if (weight(a, b) == 0) {if (weight(a, c) == 1) cout << "c是假幣,且重量輕" << endl;else cout<<" c是假幣,且重量重" << endl;}else if (weight(a, b)== 1)cout << "b是假幣,且重量輕";else cout << "b是假幣,且重量重";break;case 1: //a,d是假幣if (weight(a, c) == 1)cout << "a是假幣,且重量重" << endl;else cout << "d是假幣,且重量輕" << endl;break;default: //e,f是假幣if (weight(c, e) == 1)cout << "e是假幣,且重量輕" << endl;else cout << "f是假幣,且重量輕" << endl;}break;case 2: //a,b,c與d,e,f中有假幣,如果假幣重,則在d,e,f中,如果假幣輕,則在a,b,c中switch (weight(d + b + c, a + h + g)) { //d,b,c一組 a,h,g一組case 0: //e,f是假幣if (weight(a, e) == 0) {if (weight(a, f) == 1) cout << "f是假幣,且重量輕" << endl;else cout << " f是假幣,且重量重" << endl;}else if (weight(a, e) == 1)cout << "e是假幣,且重量輕" << endl;else cout << "e是假幣,且重量重"<<endl;break;case 1: //d,a是假幣if (weight(d, c) == 1)cout << "d是假幣,且重量重" << endl;else cout << "a是假幣,且重量輕" << endl;break;default: //b,c是假幣if (weight(a, b) == 1)cout << "b是假幣,且重量輕" << endl;else cout << "c是假幣,且重量輕" << endl;}} }總結(jié)
- 上一篇: 【转】Linux下进程隐藏的常见手法及侦
- 下一篇: html实现简单动画,编写自己的代码库(