POJ 1013 Counterfeit Dollar

来源:互联网 发布:社交网络的利与弊英语 编辑:程序博客网 时间:2024/05/18 01:43
Counterfeit Dollar
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 38774 Accepted: 12369

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. 昨天整了一下午,都没发现哪里错了,今天给重新看了一遍程序,才发现只是在(4)部分的时候,ok应该放在循环的外边,否则,Ok永远将会是0,此段程序就没有作用了。还是要多尽点心才好,昨天没找出来bug,然后就想放弃了,想着反正大多数数据都过了,就行了,可是就是不过,到时候比赛的时候遇到了这个题还是不会过啊,其实很多题都是这样被给放弃的,往往AC就只差一步。做题时候的思想就是下边这几个步骤,一步一步来的,就不在这边再重写了。然后这题收获的一个基础点是:数组下标的表示:(1)如果要调用另一个字符数组里的字符的时候,直接将这个字符数组写入就行了,不用加单引号,他传递给数组的就是它本身的ASCII值,但是要注意最好也养成习惯的是在字符数组的最后再减去'A'或'a',方便计数。     (2)如果是调用字符,则要加单引号,同时也要减去'A'或'a'。最后,坚持就是胜利。
#include <stdio.h>#include <string.h>char s1[5][20],s2[5][20],s3[5][20];int a[20];char s[]={"ABCDEFGHIJKL"};int main (void){   // freopen("1013.txt","r",stdin);    int n,i,j,k,l,ok,flag,k1,max;    while(scanf("%d",&n)!=EOF)    {        for(i=1;i<=n;i++)        {            for(j=1;j<=3;j++)            {//数组值为0的就是真的。本题中数组初始化为1,就表示出现过,再没有其他意思。(1)                scanf("%s%s%s",s1[j],s2[j],s3[j]);                l=strlen(s1[j]);                for(k=0;k<l;k++)                {                    a[s1[j][k]-'A']=1;                    a[s2[j][k]-'A']=1;                }            }            for(j=1;j<=3;j++)            {//如果s3为even,则两侧的球都是真的,即标为0.(2)                if(strcmp(s3[j],"even")==0)                {                    l=strlen(s1[j]);                    for(k=0;k<l;k++)                    {                        a[s1[j][k]-'A']=0;                        a[s2[j][k]-'A']=0;                    }                }            }            for(j=1;j<=3;j++)            {//如果两侧不平衡的话,那么没有出现在两侧的球都是真的。(3)                if(strcmp(s3[j],"even"))                {                    l=strlen(s1[j]);                     for(k=0;k<12;k++)                     {                         if(a[k])                         {                             ok=1;                           for(k1=0;k1<l;k1++)                         {                             if((('A'+k)==s1[j][k1])||(('A'+k)==s2[j][k1]))                             {                                 ok=0;                                 break;                             }                         }                         if(ok)                            a[k]=0;                         }                     }                }            }          ok=0;        for(j=1;j<=3;j++)        {            //如果该金币不确定并且天平不平衡的时候,如果可能是重的,就标记为2,如果可能是轻的,就标记为1.如果下一个不平衡的时候,由1变为2,或由2变为1的时候,则肯定是真的。标记为0.(4)            if(strcmp(s3[j],"even"))            {                l=strlen(s1[j]);                if(ok)                {                    if(strcmp(s3[j],"up")==0)                    {                        for(k=0;k<l;k++)                        {                            if(a[s1[j][k]-'A']==1) a[s1[j][k]-'A']=0;                           if(a[s2[j][k]-'A']==2) a[s2[j][k]-'A']=0;                        }                    }                    else                    {                        for(k=0;k<l;k++)                        {                            if(a[s1[j][k]-'A']==2) a[s1[j][k]-'A']=0;                           if(a[s2[j][k]-'A']==1) a[s2[j][k]-'A']=0;                        }                    }                }                else                      {                           if(strcmp(s3[j],"up")==0)                    {                        for(k=0;k<l;k++)                        {                            if(a[s1[j][k]-'A']) a[s1[j][k]-'A']=2;                           if(a[s2[j][k]-'A']) a[s2[j][k]-'A']=1;                        }                        ok=1;                    }                    else                    {                        for(k=0;k<l;k++)                        {                            if(a[s1[j][k]-'A']) a[s1[j][k]-'A']=1;                           if(a[s2[j][k]-'A']) a[s2[j][k]-'A']=2;                        }                        ok=1;                    }                      }            }        }        for(k=0;k<12;k++)            {//将所有疑似假币的金币状态初始化为1.以便后边找出出现次数最多的,即为假币。(5)                if(a[k])                    a[k]=1;            }         for(j=1;j<=3;j++)            {//找出出现次数最多的(6)                l=strlen(s1[j]);                 for(k=0;k<l;k++)                 {                     if(a[s1[j][k]-'A']) a[s1[j][k]-'A']++;                     if(a[s2[j][k]-'A']) a[s2[j][k]-'A']++;                 }            }            for(k=0;k<12;k++)            {//此时已找到假币(7)                max=0;                if(max<a[k])                {                    max=a[k];                    flag=k;                }            }            for(j=1;j<=3;j++)            {//输出(8)                if(strcmp(s3[j],"even"))                {                    l=strlen(s1[j]);                    for(k=0;k<l;k++)                    {                        if(s1[j][k]==('A'+flag))                        {                            if(strcmp(s3[j],"up")==0) printf("%c is the counterfeit coin and it is heavy.\n",'A'+flag);                            else printf("%c is the counterfeit coin and it is light.\n",'A'+flag);                            break;                        }                        if(s2[j][k]==('A'+flag))                        {                            if(strcmp(s3[j],"up")==0) printf("%c is the counterfeit coin and it is light.\n",'A'+flag);                            else printf("%c is the counterfeit coin and it is heavy.\n",'A'+flag);                            break;                        }                    }                        break;                }            }        }    }    return 0;}
0 0