原题及翻译
Counterfeit Dollar
假币
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 52569 Accepted: 16434
时限:1000毫秒 内存限制:10000K
提交总数:52569 接受:16434
Description
描述
Sally Jones has a dozen Voyageur silver dollars.
萨利·琼斯有一打旅行银元。
However, only eleven of the coins are true silver dollars; one coin is counterfeit even though its color and size make it indistinguishable from the real silver dollars.
然而,只有11个硬币是真正的银币;一个硬币是伪造的,尽管它的颜色和大小使它与真正的银币无法区分。
The counterfeit coin has a different weight from the other coins but Sally does not know if it is heavier or lighter than the real coins.
假币的重量与其他硬币的重量不同,但莎莉不知道它是否比真正的硬币重或轻。
Happily, Sally has a friend who loans her a very accurate balance scale.
幸好,莎莉有个朋友借给她一个非常精确的天平。
The friend will permit Sally three weighings to find the counterfeit coin.
这位朋友将允许萨利三次称重以找到假币。
For instance, if Sally weighs two coins against each other and the scales balance then she knows these two coins are true.
例如,如果莎莉两个硬币相对重,天平平衡,那么她就知道这两个硬币是真的。
Now if Sally weighs
如果莎莉重了
one of the true coins against a third coin and the scales do not balance then Sally knows the third coin is counterfeit and she can tell whether it is light or heavy depending on whether the balance on which it is placed goes up or down, respectively.
一枚真正的硬币与一枚第三枚硬币和天平不平衡,那么莎莉就知道第三枚硬币是伪造的,她可以根据放置它的天平是上下来判断它是轻的还是重的。
By choosing her weighings carefully, Sally is able to ensure that she will find the counterfeit coin with exactly three weighings.
通过仔细选择自己的重量,莎莉能够确保她将找到准确三个重量的假币。
Input
输入
The first line of input is an integer n (n > 0) specifying the number of cases to follow.
输入的第一行是一个整数n(n>0),指定了要遵循的事例数。
Each case consists of three lines of input, one for each weighing.
每个情况由三条输入线组成,每个称重一条。
Sally has identified each of the coins with the letters A–L.
萨利已经用字母A–L识别了每一枚硬币。
Information on a weighing will be given by two strings of letters and then one of the words up'',down’’, or ``even’’.
关于称重的信息将由两个字母串给出,然后是单词“向上”、“向下”或“偶数”中的一个。
The first string of letters will represent the coins on the left balance; the second string, the coins on the right balance.
第一串字母代表左边天平上的硬币;第二串字母代表右边天平上的硬币。
(Sally will always place the same number of coins on the right balance as on the left balance.)
(莎莉总是在右边的天平上放相同数量的硬币,就像在左边的天平上一样。)
The word in the third position will tell whether the right side of the balance goes up, down, or remains even.
第三个位置的单词会告诉天平的右边是上升、下降还是保持平衡。
Output
输出
For each case, the output will identify the counterfeit coin by its letter and tell whether it is heavy or light.
对于每种情况,输出将通过其字母识别假币,并判断它是重的还是轻的。
The solution will always be uniquely determined.
解决方案始终是唯一确定的。
Sample Input
1
ABCD EFGH even
ABCI EFJK up
ABIJ EFGH even
Sample Output
K is the counterfeit coin and it is light.
Source
East Central North America 1998
思路
对于每一枚硬币先假设它是轻的,看这样是否符合称量结果。
如果符合,问题即解决。如果不符合,就假设它是重的,看是否符合称量结果。
把所有硬币都试一遍,一定能找到特殊硬币。
代码
char left[3][7]; //左边的称量硬币
char right[3][7]; //右边的称量硬币
char result[3][7]; //称量结果
bool isfake(char c,bool light);
int main ()
{
int n;
scanf("%d",&n);
while(n--)
{
for(int i=0;i<3;++i)
{
scanf("%s%s%s",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);
break;
}
else if(isfake(c,false))
{
printf("%c is the counterfeit coin and it is heavy. \n",c);
break;
}
}
}
return 0;
}
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)||strchr(pright,c))
return false;
break;
case 'd':
if(strchr(pleft,c)==NULL)
return false;
break;
}
}
return true;
}