POJ 1013
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 27117 | Accepted: 8460 |
Description
Happily, Sally has a friend who loans her a very accurate balance scale. The friend will permit Sally three weighings to find the counterfeit coin. For instance, if Sally weighs two coins against each other and the scales balance then she knows these two coins are true. Now if Sally weighs
one of the true coins against a third coin and the scales do not balance then Sally knows the third coin is counterfeit and she can tell whether it is light or heavy depending on whether the balance on which it is placed goes up or down, respectively.
By choosing her weighings carefully, Sally is able to ensure that she will find the counterfeit coin with exactly three weighings.
Input
Output
Sample Input
1 ABCD EFGH even ABCI EFJK up ABIJ EFGH even
Sample Output
K is the counterfeit coin and it is light.
Source
AC代码
//经典模拟题 //关键思路还是假想如果硬币重或者轻时放在左右两边24种情况应该的results,一一对比 //这里先假设硬币偏重,0表示不在,1表示在左边,-1表示在右边 //硬币偏轻的情况就恰好与预想情况相反 #include <iostream> #include <string> #include <fstream> #include <memory.h> using namespace std; int main(){ int exp[12][3]; int result[3]; int n,i,j; string s1,s2,s3; cin>>n; while(n--){ memset(exp,0,sizeof(exp)); memset(result,0,sizeof(result)); //下面开始三次试验,模拟出每次的结果 for (i=0;i<3;i++) { cin>>s1>>s2>>s3; for (j=0;j<s1.size();j++) { exp[s1[j]-'A'][i]=1;//表明此币在左边 exp[s2[j]-'A'][i]=-1;//表明此币在右边 } if (s3 == "even") { result[i] = 0;//表示此时重币应该不在天平上 } else if (s3 == "down") { result[i] = -1;//表示此时重币应该在右边出现才对 } else result[i] = 1; } for (j=0;j<12;j++) { if (exp[j][0] == result[0] && exp[j][1] == result[1] && exp[j][2] == result[2]) { cout<<char('A'+j)<<" is the counterfeit coin and it is heavy."<<endl; break; } else if(exp[j][0] == -result[0] && exp[j][1] == -result[1] && exp[j][2] == -result[2]) { cout<<char('A'+j)<<" is the counterfeit coin and it is light."<<endl; break; } } } return 0; }