聪明的李思思[1元]
Time Limit : 3000/1000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 12 Accepted Submission(s) : 5
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
李思思有12枚银币。其中有11枚真币和1枚假币。假币看起来和真币没有区别,但是重量不同。但李思思不知道假币比真币轻还是重。于是他向朋友借了一架天平。朋友希望李思思称三次就能找出假币并且确定假币是轻是重。例如:如果李思思用天平称两枚硬币,发现天平平衡,说明两枚都是真的。如果李思思用一枚真币与另一枚银币比较,发现它比真币轻或重,说明它是假币。经过精心安排每次的称量,李思思保证在称三次后确定假币。
Input
第一行有一个数字n,表示有n组测试用例。对于每组测试用例: 输入有三行,每行表示一次称量的结果。李思思事先将银币标号为A-L。每次称量的结果用三个
以空格隔开的字符串表示:天平左边放置的硬币 天平右边放置的硬币 平衡状态。其中平衡状态用``up'', ``down'', 或 ``even''表示, 分别为右端高、右端低和平衡。天平左右的硬币数总是相等的。
Output
输出哪一个标号的银币是假币,并说明它比真币轻还是重(heavy or light)。
Sample Input
1
ABCD EFGH even
ABCI EFJK up
ABIJ EFGH even
Sample Output
K is the counterfeit coin and it is light.
Author
tianzuwei
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
char left[3][7];
char right[3][7];
char cmd[3][7];
bool isheavy(char c)
{
for(int i = 0 ; i <= 2;i++)
{
if(cmd[i][0] == 'e')
{
if(strchr(left[i],c)!=NULL||strchr(right[i],c)!=NULL)
{
return false;break;
}
}
else if(cmd[i][0] == 'u')
{
if(strchr(left[i],c)==NULL) {
return false;break;
}
}
else if(cmd[i][0] == 'd')
{
if(strchr(right[i],c)==NULL){
return false;break;
}
}
}
return true;
}
bool islight(char c)
{
for(int i = 0 ; i <= 2;i++)
{
if(cmd[i][0] == 'e')
{
if(strchr(left[i],c)!=NULL||strchr(right[i],c)!=NULL)
{
return false;break;
}
}
else if(cmd[i][0] == 'u')
{
if(strchr(right[i],c)==NULL) {
return false;break;
}
}
else if(cmd[i][0] == 'd')
{
if(strchr(left[i],c)==NULL){
return false;break;
}
}
}
return true;
}
int main()
{
int n;
scanf("%d",&n);
while(n--)
{
for(int i = 0; i <= 2; i++)
scanf("%s %s %s",&left[i],&right[i],&cmd[i]);
for(char ch = 'A';ch <= 'L';ch++)
{
if(islight(ch))
{
printf("%c is the counterfeit coin and it is light.\n",ch);
break;
}
else if(isheavy(ch))
{printf("%c is the counterfeit coin and it is heavy.\n",ch);
break;
}
}
}
return 0;
}