# poj 1942 Counterfeit Dollar

35 篇文章 0 订阅

Language:
Counterfeit Dollar
 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 45287 Accepted: 14316

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.

Source

在网上看到了个挺好的题解，转载一下
 Language:

Left   right     status

Up：右盘上升，说明右盘可能有轻假币，也可能左盘有重假币。

Down：右盘下降，说明右盘可能有重假币，也可能左盘有轻假币。

Even：右盘与左盘平衡，由于假币有且仅有1枚，则说明此时天枰两边的硬币全为真币。

1、  有且仅有1枚假币

2、  假币相对于真币的重量，可能轻可能重

3、  只称量3次，且称量3次恰好且必能找到假币

4、  每次称量时天枰两边的硬币数目一样

5、  选取哪些硬币称量由input决定

ac代码：
#include <stdio.h>
#include <math.h>
#include <string.h>

char yuansu={'A','B','C','D','E','F','G','H','I','J','K','L'};

int search(char e)
{
int i;

for(i=0;i<12;i++)
{
if(yuansu[i]==e)
return i;
}

return -1;
}

int main()
{
int t,i,j,k,jdg,n,flag,max,time;
char a,b,c;

scanf("%d",&t);
getchar();
while(t--)
{
for(i=0;i<12;i++)
{
jdg[i]=0;
time[i]=0;
}
for(i=0;i<3;i++)
{
scanf("%s%s%s",a[i],b[i],c[i]);
getchar();
flag=-1;
if(strcmp(c[i],"even")==0)
{
n=strlen(a[i]);
for(j=0;j<n;j++)
{
k=search(a[i][j]);
jdg[k]=1;
}
n=strlen(b[i]);
for(j=0;j<n;j++)
{
k=search(b[i][j]);
jdg[k]=1;
}
}
else if(strcmp(c[i],"up")==0)
{
n=strlen(a[i]);
for(j=0;j<n;j++)
{
k=search(a[i][j]);
time[k]++;
}
n=strlen(b[i]);
for(j=0;j<n;j++)
{
k=search(b[i][j]);
time[k]--;
}
}
else if(strcmp(c[i],"down")==0)
{
n=strlen(a[i]);
for(j=0;j<n;j++)
{
k=search(a[i][j]);
time[k]--;
}
n=strlen(b[i]);
for(j=0;j<n;j++)
{
k=search(b[i][j]);
time[k]++;
}
}
}

max=-1;
for(i=0;i<12;i++)
{
if(jdg[i])
continue;

if(max<=fabs(time[i]))
{
max=fabs(time[i]);
flag=i;
}
}

printf("%C is the counterfeit coin and it is ",yuansu[flag]);
if(time[flag]<0)
{
printf("light.\n");
}
else
{
printf("heavy.\n");
}
}

return 0;
}

Language:
Counterfeit Dollar
 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 45287 Accepted: 14316

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.

Source

04-07 318  10-21 148
02-13 585
06-02 380
04-21 164
03-08 335
04-05 86
04-15 55
01-04 22
12-10 528
03-01 462
11-12 420
11-26 391
03-07 381
02-22 372
03-21 371
01-19 345
07-18 336 点击重新获取   扫码支付  余额充值