Problem 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
Output
Sample Input
Sample Output
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int main()
{
int n;
int cont[15];
char s1[10],s2[10],s3[10];
scanf("%d",&n);
while(n--)
{
memset(cont,0,sizeof(cont));
for(int i=0;i<3;i++)
{
scanf("%s %s %s",s1,s2,s3);
for(int j=0;s1[j]!='\0';j++)
{
if(s3[0]=='e')
{
cont[s1[j]-'A']=20;
cont[s2[j]-'A']=20;
}
else if(s3[0]=='u')
{
if(cont[s1[j]-'A']!=20)
{
cont[s1[j]-'A']++;
}
if(cont[s2[j]-'A']!=20)
{
cont[s2[j]-'A']--;
}
}
else
{
if(cont[s1[j]-'A']!=20)
{
cont[s1[j]-'A']--;
}
if(cont[s2[j]-'A']!=20)
{
cont[s2[j]-'A']++;
}
}
}
}
int x=-1,maxx=0;
for(int i=0;i<12;i++)
{
if(cont[i]==20)
continue;
if(maxx<abs(cont[i]))
{
maxx=abs(cont[i]);
x=i;
}
}
if(cont[x]<0)
{
printf("%c is the counterfeit coin and it is light.\n",'A'+x);
}
else
{
printf("%c is the counterfeit coin and it is heavy.\n",'A'+x);
}
}
return 0;
}