# POJ 1013 Counterfeit Dollar C语言版

POJ题解 2736浏览

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
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)，保证可以找到假币，要求你输出假币编号并指出假币是比真币轻还是比真币重。

```#include
#include
#include
#include

int val;

/* 处理不平衡状态 */
void work(int len, char wi[], char li[])
{
int j;
for (j = 0; j < len; j++)
{
/* 对重的一边，已经是真的不需要再判断 */
if (val[wi[j]] != 10)
{
val[wi[j]]++;
}

/* 对轻的一边，已经是真的不需要再判断 */
if (val[li[j]] != 10)
{
val[li[j]]--;
}
}
}

int main()
{
char l, r, w;
int i, j, n, len, max, maxid;
scanf("%d", &n);
while(n--)
{
/* 初始化标记数组 */
for (i = 65; i < 77; i++)
{
val[i] = 0;
}
/* 对每组三次称量进行处理 */
for (i = 0; i < 3; i++)
{
scanf("%s%s%s", l[i], r[i], w[i]);
len = strlen(l[i]);
/* 如果相等则所有硬币都是真的，置10 */
if (w[i] == 'e')
{
for (j = 0; j < len; j++)
{
val[l[i][j]] = 10;
val[r[i][j]] = 10;
}
}
/* 如果左边重 */
else if (w[i] == 'u')
{
work(len, l[i], r[i]);
}
/* 如果左边轻 */
else if (w[i] == 'd')
{
work(len, r[i], l[i]);
}
}

/* 找出哪个硬币被怀疑最多次 */
max = maxid = 0;
for (i = 65; i < 77; i++)
{
if (val[i] != 10 && abs(val[i]) > max)
{
max = abs(val[i]);
maxid = i;
}
}

/* 输出假币信息 */
if (val[maxid] > 0)
{
printf("%c is the counterfeit coin and it is heavy.\n", maxid);
}
else
{
printf("%c is the counterfeit coin and it is light.\n", maxid);
}
}
//system("pause");
return 0;
}
```

### 网友最新评论 (1)

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