poj1013
题意:有12个硬币,其中有且仅有一个是假的,假的比真的或轻或重,给出3次天平测量的结果,每次告知左侧和右侧的硬币各有那几个,以及天平的平衡状态,问第几个硬币是假的,是轻是重。(保证结果唯一)
分析:对于每次测量,如果天平平衡则说明所有天平上的硬币都是真的,如果两端不平衡,那么轻的一端的所有硬币只可能是轻的假币,不可能是重的假币。重的一端同理。于是我们知道哪些硬币一定是真币,哪些硬币一定不是重的假币也不是轻的假币,则这些也是真币。除此之外,剩下的那一个就是假币,如果它不可能轻,那么它一定重,反之亦然。
View Code
#include <iostream> #include <string> using namespace std; struct { bool maybelight, maybeheavy, mustbenormal; }coin[13]; int n; void init() { string left, right, judge; int j, i; memset(coin, 0, sizeof(coin)); for (i = 0; i < 3; i++) { cin >> left; cin >> right; cin >> judge; if (judge == "even") { for (j = 0; j < left.length(); j++) coin[left[j] - 'A'].mustbenormal = true; for (j = 0; j < right.length(); j++) coin[right[j] - 'A'].mustbenormal = true; }else if (judge == "down") { for (j = 0; j < left.length(); j++) coin[left[j] - 'A'].maybelight = true; for (j = 0; j < right.length(); j++) coin[right[j] - 'A'].maybeheavy = true; for (j = 0; j <= 'L' - 'A'; j++) if (left.find('A' + j) == string::npos && right.find('A' + j) == string::npos) coin[j].mustbenormal = true; }else if (judge == "up") { for (j = 0; j < left.length(); j++) coin[left[j] - 'A'].maybeheavy = true; for (j = 0; j < right.length(); j++) coin[right[j] - 'A'].maybelight = true; for (j = 0; j <= 'L' - 'A'; j++) if (left.find('A' + j) == string::npos && right.find('A' + j) == string::npos) coin[j].mustbenormal = true; } } } void work() { int i, ans; for (i = 0; i <= 'L' - 'A'; i++) { if (coin[i].mustbenormal || (coin[i].maybeheavy && coin[i].maybelight)) continue; ans = i; } if (coin[ans].maybeheavy) printf("%c is the counterfeit coin and it is heavy.\n", ans + 'A'); else printf("%c is the counterfeit coin and it is light.\n", ans + 'A'); } int main() { //freopen("t.txt", "r", stdin); cin >> n; getchar(); while (n--) { init(); work(); } return 0; }