Counterfeit Dollar
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 37192 | Accepted: 11900 |
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.
如果是even那么里面所有的都是真的,如果是up和down,那么不在里面的都是真的。如果是up,那么在左边的都有可能是重的,在右边的都有可能是轻的。如果是的down,那么在左边的都有可能是轻的,在右边的都有可能是轻重的,如果可能是重的和轻的,那么它也是真的!这样排除了所有的情况之后剩下的就是假的硬币了!
#include<stdio.h>
#include<cstring>
#include<algorithm>
#define Max(x,y) x>y?x:y
using namespace std;
void print(int *s)
{
int i;
for(i=0;i<12;i++)
printf("%d ",s[i]);
puts("");
}
int real[12],no[2][12],vis[12],have[12];
char s[3][3][14];
int main()
{
int i,j,t;
//freopen("D:\\o.txt","r",stdin);
scanf("%d",&t);
getchar();
while(t--)
{
memset(real,0,sizeof(real));
memset(no,0,sizeof(no));
memset(have,0,sizeof(have));
for(i=0;i<3;i++)
{
scanf("%s%s%s",s[i][0],s[i][1],s[i][2]);
if(s[i][2][0]=='u')
{
for(j=0;j<strlen(s[i][0]);j++)
no[0][s[i][0][j]-'A']=1;//不会含有轻的硬币
for(j=0;j<strlen(s[i][1]);j++)
no[1][s[i][1][j]-'A']=1;//不会含有重的硬币
for(j=0;j<12;j++)//不在里面的都是真的
{
if(strchr(s[i][0],j+'A')==NULL&&strchr(s[i][1],'A'+j)==NULL)
real[j]=1;
}
}
else if(s[i][2][0]=='d')
{
for(j=0;j<strlen(s[i][1]);j++)
no[1][s[i][0][j]-'A']=1;//不会含有重的硬币
for(j=0;j<strlen(s[i][0]);j++)
no[0][s[i][1][j]-'A']=1;//不会含有轻的硬币
for(j=0;j<12;j++)
{
if(strchr(s[i][0],j+'A')==NULL&&strchr(s[i][1],'A'+j)==NULL)
real[j]=1;
}
}
else if(s[i][2][0]=='e')//都是真的硬币,并且所有的硬币都相等
{
for(j=0;j<strlen(s[i][0]);j++)
real[s[i][0][j]-'A']=1;
for(j=0;j<strlen(s[i][1]);j++)
real[s[i][1][j]-'A']=1;
}
}
int ok=0,ans;
for(i=0;i<12;i++)
{
if(real[i]||no[0][i]&&no[1][i])continue;
ans=i;
if(no[0][i]==0&&no[1][i])//不重
ok=1;
else if(no[0][i]&&no[1][i]==0)//不轻
ok=0;
}
if(ok)
printf("%c is the counterfeit coin and it is light.\n",ans+'A');
else printf("%c is the counterfeit coin and it is heavy.\n",ans+'A');
}
return 0;
}