C语言编程问题求解答

题目描述
赛利有12枚银币。其中有11枚真币和1枚假币。假币看起来和真币没有区别,但是重量不同。但赛利不知道假币比真币轻还是重。于是他向朋友借了一架天平。朋友希望赛利称三次就能找出假币并且确定假币是轻是重。例如:如果赛利用天平称两枚硬币,发现天平平衡,说明两枚都是真的。如果赛利用一枚真币与另一枚银币比较,发现它比真币轻或重,说明它是假币。经过精心安排每次的称量,赛利保证在称三次后确定假币。

关于输入
第一行是n,表示数据共有n组。
其后是n*3行。每组数据有三行,每行表示一次称量的结果。赛利事先将银币标号为A-L。每次称量的结果用三个以空格隔开的字符串表示:天平左边放置的硬币 天平右边放置的银币 平衡状态。其中平衡状态用"up", "down", 或 "even"表示, 分别为右端高、右端低和平衡。天平左右的银币数总是相等的。

关于输出
输出为n行。每行输出一组数据中哪一个标号的银币是假币,并说明它比真币轻还是重。
如果第K枚银币是假,并且它是轻的,则输出:
K is the counterfeit coin and it is light.
如果第K枚银币是假,并且它是重的,则输出:
K is the counterfeit coin and it is heavy.

例子输入
1
ABCD EFGH even
ABCI EFJK up
ABIJ EFGH even
例子输出
K is the counterfeit coin and it is light.
提示信息
K is the counterfeit coin and it is light.


#include <cstdio>
#include <cstring>

char Left[3][8], Right[3][8], Result[3][8];
int status[12];

bool test(){
    int left, right;
    for(int i = 0; i < 3; i++){
        left = right = 0;
        for(int j = 0; j < 6 && Left[i][j] != 0; j ++){
            left += status[Left[i][j] - 'A'];
            right += status[Right[i][j] - 'A'];
        }
        if(left > right && Result[i][0] != 'u')
            return false;
        if(left < right && Result[i][0] != 'd')
            return false;
        if(left == right && Result[i][0] != 'e')
            return false;
    }
    return true;
}


int main(){
    int n;
    scanf("%d", &n);
    while(n--){
        for(int i = 0; i < 3; i++){
            scanf("%s%s%s", Left[i], Right[i], Result[i]);
        }
        memset(status, 0, sizeof(status));
        int i;
        for(i = 0; i < 12; i++){
            status[i] = -1;
            if(test())
                break;
            status[i] = 1;
            if(test())
                break;
            status[i]= 0;
        }
        printf("%c is the counterfeit coin and it is %s.\n", i + 'A', status[i] > 0 ? "heavy" : "light");
    }
    return 0;
}