Description
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
Output
Sample Input
1 ABCD EFGH even ABCI EFJK up ABIJ EFGH even
Sample Output
K is the counterfeit coin and it is light.
#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#define MAX 1000005
using namespace std;
int main()
{
int n;
cin>>n;
int coin[26];//若等于10则说明是真的硬币
char left[13],right[13],status[5];
while(n--)
{
memset(coin,0,sizeof(coin));
for(int i=0;i<3;i++)
{
scanf("%s%s%s",left,right,status);
int length=strlen(left);
if(strcmp(status,"even")==0)
{
for(int j=0;j<length;j++)
{
coin[left[j]-'A']=10;
coin[right[j]-'A']=10;
}
}
else if(strcmp(status,"up")==0)
{
for(int j=0;j<length;j++)
{
if(coin[left[j]-'A']!=10)
coin[left[j]-'A']++;
if(coin[right[j]-'A']!=10)
coin[right[j]-'A']--;
}
}
else if(strcmp(status,"down")==0)
{
for(int j=0;j<length;j++)
{
if(coin[left[j]-'A']!=10)
coin[left[j]-'A']--;
if(coin[right[j]-'A']!=10)
coin[right[j]-'A']++;
}
}
}
int max=0,k;
for(int i=0;i<12;i++)
{
if(coin[i]==10)
continue;
if(max<=abs(coin[i])){
max=abs(coin[i]);
k=i;
}
}
if(coin[k]<0)
printf("%c is the counterfeit coin and it is light.\n",'A'+k);
else printf("%c is the counterfeit coin and it is heavy.\n",'A'+k);
}
return 0;
}