题目:
代码:
#include<iostream>
#include<cstring>
using namespace std;
char Left[3][7];
char Right[3][7];
char result[3][7];
//枚举每个硬币(c)
//假设硬币是较轻的假币(light为true)或较重的假币(light为false)
//带入看是否符合输入
//若是假币则返回true
bool IsFake(char c,bool light){
for(int i=0;i<3;i++){
char * pleft,*pright;
//如果假币是较轻的
if(light){
pleft = Left[i];
pright = Right[i];
}
//如果假币是较重的
else{
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)!=NULL || strchr(pright,c)!=NULL)
return false;
break;
case 'd':
if(strchr(pleft,c) == NULL)
return false;
break;
}
}
return true;
}
int main(){
for(int i=0;i<3;i++) {
cin>>Left[i]>>Right[i]>>result[i];
//cout << Left[i]<<" "<<Right[i]<<" "<<result[i]<<endl;
}
for(char c='A';c<='L';c++){
cout<<"假设"<<c<<"是较轻的假币"<<endl;
if(IsFake(c,true)){
cout << c <<" is the light one!";
break;
}
cout<<"假设"<<c<<"是较轻的假币无效"<<endl;
cout<<"假设"<<c<<"是较重的假币"<<endl;
if(IsFake(c,false)){
cout << c <<" is the heavy one!";
break;
}
cout<<"假设"<<c<<"是较重的假币无效"<<endl;
}
return 0;
}