poj1013
一道简单题,思路如下:
1、对每个硬币设三个bool位进行标识,分别为平衡even,重heavy,轻light;
2、读入数据,对硬币状态进行标记,这里有一个细节是,如果天枰左偏或右偏,要将没有上天枰的硬币的even位都标记为真;
3、如果硬币even位为真,或者硬币的heavy和light位同为真,那么这个硬币是真硬币,否则就是假硬币。
题设中的“The solution will always be uniquely determined.”为这种解法提供了保证。
#include <iostream> #include <string> using namespace std; struct Poss{ bool even; bool heavy; bool light; }; int main() { int n; cin >> n; while (n--){ string left, right, result; Poss arr[12]; memset(arr, 0, sizeof(arr)); for (int i = 0; i < 3; i++){ cin >> left >> right >> result; int len = left.length(); if (result == "even"){ for (int i = 0; i < len; i++){ arr[left[i] - 'A'].even = true; arr[right[i] - 'A'].even = true; } } else{ bool on[12] = { false }; for (int i = 0; i < len; i++){ on[left[i] - 'A'] = true; on[right[i] - 'A'] = true; } for (int i = 0; i < 12; i++){ if (!on[i]){ arr[i].even = true; } } if (result == "up"){ for (int i = 0; i < len; i++){ arr[left[i] - 'A'].heavy = true; arr[right[i] - 'A'].light = true; } } else{ for (int i = 0; i < len; i++){ arr[right[i] - 'A'].heavy = true; arr[left[i] - 'A'].light = true; } } } } for (int i = 0; i < 12; i++){ if (arr[i].even || (arr[i].heavy && arr[i].light)) continue; if (arr[i].heavy){ cout << char('A' + i) << " is the counterfeit coin and it is heavy." << endl; break; } else if (arr[i].light){ cout << char('A' + i) << " is the counterfeit coin and it is light." << endl; break; } } } return 0; }
posted on 2015-10-10 19:12 caiminfeng 阅读(342) 评论(0) 编辑 收藏 举报