此題沒思路,是向一位自稱菜鳥的牛人學來的。但是這個思想很巧妙
要判斷并找出A到L的字母哪個重量不標準,或者輕或者重。從A到L把所有字母掃一遍,每個字母判斷它出現的位置,如果在even中出現,則掃下一個字母,如果在up(或down)中出現,則可通過在left還是right中出現來判斷出輕重,因為只有一個重量不標準的字母,所以可以直接判斷。
        這里用到了一個很好用的字符串函數strchr(char *a ,char c),a代表所要搜索的字符串數組,c代表想要在a中查找到的字母。即在字符串a中查找有沒有字母c,如果沒有則返回NULL也就是0。很好用的函數啊^-^

還有一點很奇怪,這個頭文件必須用#include<stdio.h>如果用#include<iostream>在Dev-cpp中會出現“'right' undeclared(first use this function)”可是我明明定義的全局變量right[3][10]啊;在VC++中就沒有錯誤,難道編譯器太嚴格了?不理解……


Source Code

Problem: 1013 User: wic
Memory: 180K Time: 0MS
Language: C++ Result: Accepted
  • Source Code
    #include<stdio.h>
        #include<string.h>
        char left[3][10],right[3][10],judge[3][10];
        bool light(char c)
        {
        int i;
        for(i=0; i<3; i++)
        switch(judge[i][0])
        {
        case'u':if(strchr(right[i],c)==NULL)return false;break;
        case'e':if(strchr(left[i],c)!=NULL||strchr(right[i],c)!=NULL)return false; break;
        case'd':if(strchr(left[i],c)==NULL)return false; break;
        }
        return true;
        }
        bool heavy(char c)
        {
        int i;
        for(i=0; i<3; i++)
        switch(judge[i][0])
        {
        case'd':if(strchr(right[i],c)==NULL)return false;break;
        case'e':if(strchr(left[i],c)!=NULL||strchr(right[i],c)!=NULL)return false; break;
        case'u':if(strchr(left[i],c)==NULL)return false; break;
        }
        return true;
        }
        int main()
        {
        int n,i;
        char c;
        scanf("%d",&n);
        while(n--){
        for(i=0; i<3; i++)
        scanf("%s%s%s",left[i],right[i],judge[i]);
        for(c='A'; c<='L'; c++){
        if(light(c))
        {printf("%c is the counterfeit coin and it is light.\n",c); break;}
        if(heavy(c))
        {printf("%c is the counterfeit coin and it is heavy.\n",c); break; }
        }
        }
        return 0;
        }