首页 文章详情

poj 1013 Counterfeit Dollar

ACM比赛整理 | 131 2021-10-16 09:33 0 0 0
UniSMS (合一短信)

Counterfeit Dollar


Time Limit: 1000MS
Memory Limit: 10000K
Total Submissions: 58279
Accepted: 18002

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. 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. Each case consists of three lines of input, one for each weighing. Sally has identified each of the coins with the letters 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.



伪造的美元


内存限制:10000K


总提交:58279接受:18002


描述




     萨莉·琼斯有一打航海者银币。然而,其中只有11枚是真正的银元;有一枚硬币是假币,尽管它的颜色和大小与真银币难以区分。假硬币的重量与其他硬币不同,但萨利不知道它比真硬币更重还是更轻。

    幸好莎莉有个朋友借给她一个非常精确的天平。朋友将允许莎莉称重三次,以找到假币。举个例子,如果Sally把两枚硬币相对称,而天平是平衡的,那么她就知道这两枚硬币是真的。如果莎莉称重

    一枚真硬币和第三枚硬币的天平不平衡,然后莎莉知道第三枚硬币是假的,她可以根据放置它的天平是否上升或下降来判断它是轻还是重。

通过仔细选择秤重,Sally能够确保她将找到正巧有三个秤重的假币。


输入

    输入的第一行是一个整数n (n > 0),指定接下来的情况数。每个案例由三行输入组成,每一行输入用于称重。莎莉辨认出了每一枚带有字母A- L的硬币。有关称重的信息将由两串字母和一个单词“上”、“下”或“甚至”组成。第一串字母将代表左边平衡的硬币;第二根弦,硬币在正确的平衡。(Sally总是把相同数量的硬币放在右边的平衡和左边的平衡。)位于第三个位置的单词将告诉你平衡的右侧是上升,下降,还是保持平衡。


输出

    对于每一种情况,输出器将根据假币的字母识别假币,并告诉它是重还是轻。解总是唯一确定的。


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.



代码:

#include<stdio.h>
#include<string.h>
#include<math.h>
int Abs(int a)
{
return a>0?a:(-a);
}
int vis[15];
int main()
{
int i,j,T,k;int ans;
char str1[10],str2[10],str3[10];
scanf("%d",&T);
getchar();
while(T--)
{
ans=0;
for(i=1;i<=12;i++)
{
vis[i]=10;
}
for(k=1;k<=3;k++)
{
memset(str1,0,sizeof(str1));
memset(str2,0,sizeof(str2));
memset(str3,0,sizeof(str3));
scanf("%s %s %s",str1,str2,str3);
if(strcmp(str3,"even")==0)
{
for(i=0;str1[i]!='\0';i++)
vis[str1[i]-'A'+1]=0;
for(i=0;str2[i]!='\0';i++)
vis[str2[i]-'A'+1]=0;
}
else if(strcmp(str3,"up")==0)
{
for(i=0;str1[i]!='\0';i++)
if(vis[str1[i]-'A'+1]!=0)
vis[str1[i]-'A'+1]++;//左边重假币
for(i=0;str2[i]!='\0';i++)
if(vis[str2[i]-'A'+1]!=0)
vis[str2[i]-'A'+1]--;//右边轻假币
}
else
{
for(i=0;str1[i]!='\0';i++)
if(vis[str1[i]-'A'+1]!=0)
vis[str1[i]-'A'+1]--;//左边轻假币
for(i=0;str2[i]!='\0';i++)
if(vis[str2[i]-'A'+1]!=0)
vis[str2[i]-'A'+1]++;//右边重假币
}
ans=0;
for(i=1;i<=12;i++)
{
if(Abs(vis[i]-10)>Abs(ans)&&vis[i]!=0)
{
ans=vis[i]-10;
j=i;
}
}
}
if(ans>0)
printf("%c is the counterfeit coin and it is heavy.\n",j+64);
else
printf("%c is the counterfeit coin and it is light.\n",j+64);
}
return 0;
}


good-icon 0
favorite-icon 0
收藏
回复数量: 0
    暂无评论~~
    Ctrl+Enter