http://poj.org/problem?id=1013
这是一道把枚举思想用到极致了。
题意百度上太多了,不解释。
思路:想要找出哪一个硬币是假的枚举 A 到 L 的每一种情况。每一种情况要枚举是轻还是重。
判断轻重枚举三种条件。
如x是轻的:
1:even的情况是 Left 和 Right 都没有出现x。
2:up 的情况是 Right里面出现x。
3:down的情况是 Left 里面存在x。
#include<iostream>
#include<cstring>
using namespace std;
char Left[3][10],Right[3][10],Result[3][10];
int isLight(char x)
{
for(int i = 0;i < 3; i++)
switch(Result[i][0]){
case 'u' : if(strchr(Right[i],x) == NULL) return false;break;
case 'e' : if(strchr(Left[i],x) != NULL || strchr(Right[i],x) != NULL) return false;break;
case 'd' : if(strchr(Left[i],x) == NULL) return false;break;
}
return true;
}
int isHeavy(char x)
{
for(int i = 0;i < 3; i++)
switch(Result[i][0]){
case 'u' : if(strchr(Left[i],x) == NULL) return false;break;
case 'e' : if(strchr(Left[i],x) != NULL || strchr(Right[i],x) != NULL) return false;break;
case 'd' : if(strchr(Right[i],x) == NULL) return false;break;
}
return true;
}
int main()
{
int n;
cin>>n;
while(n--){
for(int i = 0;i < 3; i++)
cin>>Left[i]>>Right[i]>>Result[i];
char ans;
for(ans = 'A';ans <= 'L'; ans++){
if(isLight(ans)){
cout<<ans<<" is the counterfeit coin and it is light."<<endl;
break;
}
if(isHeavy(ans)){
cout<<ans<<" is the counterfeit coin and it is heavy."<<endl;
break;
}
}
}
return 0;
}