Poj百练 2692:假币问题 (分类:模拟)

//poj2692假币问题
#include <iostream>
#include<cstdio>
#include<string.h>
#include<cstdarg>
using namespace std;

int n;
int status[12]; //每个硬币的状态
char lef[3][7], righ[3][7], res[3][7];

bool balance()
{
    for(int i = 0; i < 3; i++){
        int rightW = 0, leftW = 0;
        for(int j = 0; righ[j] != 0 && j < 6; j++)
            rightW += status[righ[i][j]-'A'];
        for(int j = 0; left[j] != 0 && j < 6; j++)
            leftW += status[lef[i][j]-'A'];
        if(res[i][0] == 'e' && rightW != leftW)
            return false;
        if(res[i][0] == 'u' && rightW >= leftW)
            return false;
        if(res[i][0] == 'd' && rightW <= leftW)
            return false;
    }
    return true;
}

int main()
{//0表示未遍历,1表示为真,-1表示可能为假且轻,1表示可能为假且重
    scanf("%d",&n);
    char ans[n+2][43];
    for(int index = 0; index < n; index++){
        memset(status, 0, sizeof(status));
        memset(lef, 0, sizeof(lef));
        memset(righ, 0, sizeof(righ));
        for(int i = 0; i < 3; i++)
            scanf("%s %s %s", lef[i], righ[i], res[i]);
        for(int i = 0; i < 12; i++){
            //分别假设每个硬币为轻,重,真,依次枚举,如果满足条件则跳出
            status[i] = -1;
            if(balance()){
                strcpy(ans[index], "0 is the counterfeit coin and it is light. ");
                ans[index][0] = 'A'+i;
                break;
            }
            status[i] = 1;
            if(balance()){
                strcpy(ans[index], "0 is the counterfeit coin and it is heavy. ");
                ans[index][0] = 'A'+i;
                break;
            }
            status[i] = 0;
        }
    }

    for(int i = 0; i < n; i++)
        printf("%s\n",ans[i]);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/a1097304791/article/details/81459447