题目
输入输出样例
分析
采取枚举的方法,从A-L一个一个试,假设是假硬币,再假设是轻的还是重的
在判断是否为假硬币时:若假硬币是轻的,在右边,那么右边低,依次以这样的逻辑进行判断
代码
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
char Left[3][7]; //天平左边硬币
char Right[3][7];//天平右边硬币
char result[3][7];//结果
bool isFake(char c, bool light);
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++){
//依次从A-L遍历
if(isFake(c, true)){//假设硬币c是假的,并且轻
cout << c << " is the conterfeit coin and it's light" << endl;
}
else if(isFake(c, false)){//假设硬币c是假的,并且重
cout << c << " is the conterfeit coin and it's heavy" << endl;
}
}
}
return 0;
}
bool isFake(char c, bool light){
//假设c是假币,默认为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) || strchr(pRight,c))
return false;
case 'd':
if(strchr(pLeft,c) == NULL)
return false;
default:
break;
}
}
return true;
}