枚举——2692假币问题
枚举分析
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
char Left[3][7];
char Right[3][7];
char Result[3][7];
bool IsFake(char,bool);//返回值为真表示是一枚假币,另一个参数代表比真币重还是轻
int main(){
int t;
cin >> t;
while(t--){
for(int i = 0;i < 3; ++i)
cin >> Left[i] >> Right[i] >> Result[i];
for(char c = 'A';c <= 'L'; ++c){
if(IsFake(c,true))
printf("%c is the counterfeit coin and it is light.\n",c);
else if(IsFake(c,false))
printf("%c is the counterfeit coin and it is heavy.\n",c);
}
}
return 0;
}
bool IsFake(char c,bool light){//light为真表示假币比真币轻
for(int i = 0;i < 3; ++i){
char *pLeft,*pRight;
if(light == true){
pLeft = Left[i];
pRight = Right[i];
}
else if(light == false){//换位置免去多余比较代码
pLeft = Right[i];
pRight = Left[i];
}
switch(Result[i][0]){
case 'u':
if(strchr(pRight,c) == NULL)
return false;
break;
case 'e':
if(strchr(pLeft,c) || strchr(pRight,c))
return false;
break;
case 'd':
if(strchr(pLeft,c) == NULL)
return false;
break;
}
}
return true;
}