假币问题
总时间限制:
1000ms
内存限制:
65536kB
描述
赛利有12枚银币。其中有11枚真币和1枚假币。假币看起来和真币没有区别,但是重量不同。但赛利不知道假币比真币轻还是重。于是他向朋友借了一架天平。朋友希望赛利称三次就能找出假币并且确定假币是轻是重。例如:如果赛利用天平称两枚硬币,发现天平平衡,说明两枚都是真的。如果赛利用一枚真币与另一枚银币比较,发现它比真币轻或重,说明它是假币。经过精心安排每次的称量,赛利保证在称三次后确定假币
输入
第一行有一个数字n,表示有n组测试用例。
对于每组测试用例:
输入有三行,每行表示一次称量的结果。赛利事先将银币标号为A-L。每次称量的结果用三个以空格隔开的字符串表示:天平左边放置的硬币 天平右边放置的硬币 平衡状态。其中平衡状态用up'',
down’’, 或 ``even’'表示, 分别为右端高、右端低和平衡。天平左右的硬币数总是相等的。
输出
输出哪一个标号的银币是假币,并说明它比真币轻还是重(heavy or light)。
样例输入
1
ABCD EFGH even
ABCI EFJK up
ABIJ EFGH even
样例输出
K is the counterfeit coin and it is light.
这条题目很明显是一条简单的枚举的题目,我们只需要对每次称量的情况的每个字符进行枚举。
先假设他是假币,在分类讨论它是否比真的轻或者重
处理时候,为了避免写重复的代码
if (light)
{
pl = Left[i];
pr = Right[i];
}
else
{
pl = Right[i];
pr = Left[i];
}
//假币问题
#include <iostream>
#include <cstring>
#include <cmath>
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++)
{
if (isFake(c, true))
{
cout << c << " is the counterfeit coin and it is light. " << endl;
break;
}
else if (isFake(c, false))
{
cout << c << " is the counterfeit coin and it is heavy. " << endl;
break;
}
}
}
return 0;
}
bool isFake(char c, bool light)
{
for (int i = 0; i < 3; ++i)
{
char* pl, * pr;
if (light)
{
pl = Left[i];
pr = Right[i];
}
else
{
pl = Right[i];
pr = Left[i];
}
switch (result[i][0])
{
case 'u':
if (strchr(pr, c) == NULL)
return false;
break;
case 'e':
if (strchr(pr, c) || strchr(pl, c))
return false;
break;
case 'd':
if (strchr(pl, c) == NULL)
return false;
break;
}
}
return true;
}