最新消息:点击查看大S的省钱秘笈

POJ 1013 Counterfeit Dollar C语言版

POJ题解 Slyar 92浏览 0评论

文章作者:姜南(Slyar) 文章来源:Slyar Home (www.slyar.com) 转载请注明,谢谢合作。

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

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

Sample Output

K is the counterfeit coin and it is light.
I is the counterfeit coin and it is heavy.
I is the counterfeit coin and it is light.
L is the counterfeit coin and it is light.
B is the counterfeit coin and it is light.
A is the counterfeit coin and it is heavy.
A is the counterfeit coin and it is light.
L is the counterfeit coin and it is heavy.
A is the counterfeit coin and it is light.
A is the counterfeit coin and it is heavy.
L is the counterfeit coin and it is light.
K is the counterfeit coin and it is heavy.

Slyar:说下题目大意。至多有12个硬币,编号A-L,里面有一枚假币。多组数据,每组给出三次称量结果(even,up,down),保证可以找到假币,要求你输出假币编号并指出假币是比真币轻还是比真币重。

起初我的思路是这样的:如果是even,则两边所有的硬币都是真的;否则就对不确定的硬币记录怀疑(up或者down),如果某硬币之前已经记录过怀疑,且这次怀疑与之前的怀疑相反,则该硬币也一定是真的;最后剩下的不确定硬币就是假币了。

这样写了一次WA了,后来发现虽然题目中说“一定可以找到假币”,但实际上有些情况还是得不到唯一的假币...囧,改了算法后AC:

起初对所有硬币标记0;如果是even,则两边所有的硬币都是真的(记10);否则就对不确定的硬币记录怀疑(++或者--);最后只要看哪个硬币的绝对值最大,也就是被怀疑的次数最多,即是假币。

输入数据以及输出数据是我修改的,帮助测试。

转载请注明:Slyar Home » POJ 1013 Counterfeit Dollar C语言版

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

网友最新评论 (1)

  1. 假币比其他硬币都重或都轻,找出最重或最轻的硬币即可。如果是even,两边的硬币赋的值绝对值大于3即可。
    匿名6年前 (2010-10-09)回复