Counterfeit Dollar
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 29572 | Accepted: 9267 |
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.
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.
Source
East Central North America 1998
杯具啊,这题WA了三四遍才过,
杯具啊,这题WA了三四遍才过,
最初想法:也就是说,
1.出现在even式中的两边都是真币
2.一个硬币在不同称重中,分别属于偏重和偏轻的一端,则它是真币
3.如果三次称重后,不是真币的就输出偏重或者偏轻 结果是WA,事实上,只要修改一下第三点就可以了
3.三次称重后,如果不是真币,并且偏重或者偏轻的次数等于称重不是even的次数,就输出偏重或者偏轻 分析:我们容易觉得,如果满足最初那3点,还没有被排除的,就可以断定是假币。事实上,我们忘了另一个必要条件:该假币偏重或者偏轻的次数应该==称重不平衡的次数。原因很简单:在每次称重不平衡的时候,假币都会出现。 因此应该加上这个判断,才可以。所以可能有多个假币在前面三步还没有被排除,再加这个条件就可以了。
设置两个数组: real[12]-标志为真 lh[12]--标志被怀疑 每次称球的时候,如果是"even"则把对应的设置为"真东西",即置为1, 如果是"up"或"donw" 则把表示轻重的数组lh对应的 ++ 或者 --,直到最后。 然后把所有对应real中为1(即就是真东西啦)的lh置为0;那么操作之后, lh中存在没有辨认出真的,就是一系列的例如: -1,-2,1,2,3等数值,那么 假东西就是其中绝对值最大的那个!!------被怀疑次数最多,所以它为假。
附带下测试数据,不过的大牛可以测下:
12
ABCD EFGH even
ABCI EFJK up
ABIJ EFGH even
AGHL BDEC even
JKI ADE up
J K even
ABCDEF GHIJKL up
ABC DEF even
I J down
ABCDEF GHIJKL up
ABHLEF GDIJKC down
CD HA even
A B up
B A down
A C even
A B up
B C even
DEFG HIJL even
ABC DEJ down
ACH IEF down
AHK IDJ down
ABCD EFGH even
AB IJ even
A L down
EFA BGH down
EFC GHD even
BA EF down
A B up
A C up
L K even
ACEGIK BDFHJL up
ACEGIL BDFHJK down
ACEGLK BDFHJI down
ACEGIK BDFHJL up
ACEGIL BDFHJK down
ACEGLK BDFHJI up
代码:
#include<stdio.h>
#include<string.h>
#include<math.h>
int main()
{
int real[12],flag[12],i,k,max,t;
char str1[12],str2[12],str[12];
int n;
scanf("%d",&n);
while(n--)
{
memset(flag,0,sizeof(flag));
memset(real,0,sizeof(real));
for(k=0;k<3;k++)
{
scanf("%s %s %s",str1,str2,str);
if(strcmp(str,"even")==0)
{
for(i=0;str1[i];i++)
real[str1[i]-'A']=1;
for(i=0;str2[i];i++)
real[str2[i]-'A']=1;
}
else if(strcmp(str,"up")==0)
{
for(i=0;str1[i];i++)
flag[str1[i]-'A']++;
for(i=0;str2[i];i++)
flag[str2[i]-'A']--;
}
else{
for(i=0;str1[i];i++)
flag[str1[i]-'A']--;
for(i=0;str2[i];i++)
flag[str2[i]-'A']++;
}
}
for(i=0;i<12;i++)
if(real[i]==1)flag[i]=0;
max=0;t=0;
for(i=0;i<12;i++)
if(abs(max)<abs(flag[i])){max=flag[i];t=i;}
if(max<0)
printf("%c is the counterfeit coin and it is light.\n",t+'A');
else printf("%c is the counterfeit coin and it is heavy.\n",t+'A');
}
return 0;
}