由天平称三次的结果
来判断十二枚硬币中的1枚假币,分析:
相等说明都是真币,up/down则增加其怀疑,记为1/-1;最后遍历输出怀疑度绝对值最高的
#include<stdio.h>
#include<string.h>
int main()
{
int n;
scanf("%d\n",&n);
while (n--)
{
char s[100]="";
int i,j,l,a[30]={0},b[30]={0},maxj,max=0;
for (i=1;i<=3;i++)
{
gets(s);
l=strlen(s)-1;
if (s[l]=='n'&&s[l-1]=='e')
for (j=0;j<=l-5;j++)
if (s[j]!=' ') {a[s[j]-'A']=0;b[s[j]-'A']=1;}
if (s[l]=='p')
{
for (j=0;j<=l-3;j++)
if (s[j]!=' ') a[s[j]-'A']++;
else break;
for (;j<=l-3;j++)
if (s[j]!=' ') a[s[j]-'A']--;
}
if (s[l]=='n'&&s[l-1]=='w')
{
for (j=0;j<=l-5;j++)
if (s[j]!=' ') a[s[j]-'A']--;
else break;
for (;j<=l-5;j++)
if (s[j]!=' ') a[s[j]-'A']++;
}
}
for (j=0;j<=12;j++)
if (b[j]==0&&max<abs(a[j]))
{
max=abs(a[j]);
maxj=j;
}
printf("%c is the counterfeit coin and it is ",'A'+maxj);
if (a[maxj]>0) printf("heavy.\n");else printf("light.\n");
}
}