poj1013

题意:有12个硬币,其中有且仅有一个是假的,假的比真的或轻或重,给出3次天平测量的结果,每次告知左侧和右侧的硬币各有那几个,以及天平的平衡状态,问第几个硬币是假的,是轻是重。(保证结果唯一)

分析:对于每次测量,如果天平平衡则说明所有天平上的硬币都是真的,如果两端不平衡,那么轻的一端的所有硬币只可能是轻的假币,不可能是重的假币。重的一端同理。于是我们知道哪些硬币一定是真币,哪些硬币一定不是重的假币也不是轻的假币,则这些也是真币。除此之外,剩下的那一个就是假币,如果它不可能轻,那么它一定重,反之亦然。

View Code
#include <iostream>
#include <string>
using namespace std;

struct
{
    bool    maybelight, maybeheavy, mustbenormal;
}coin[13];

int        n;

void init()
{
    string    left, right, judge;
    int        j, i;

    memset(coin, 0, sizeof(coin));
    for (i = 0; i < 3; i++)
    {
        cin >> left;
        cin >> right;
        cin >> judge;
        if (judge == "even")
        {
            for (j = 0; j < left.length(); j++)
                coin[left[j] - 'A'].mustbenormal = true;
            for (j = 0; j < right.length(); j++)
                coin[right[j] - 'A'].mustbenormal = true;
        }else if (judge == "down")
        {
            for (j = 0; j < left.length(); j++)
                coin[left[j] - 'A'].maybelight = true;
            for (j = 0; j < right.length(); j++)
                coin[right[j] - 'A'].maybeheavy = true;
            for (j = 0; j <= 'L' - 'A'; j++)
                if (left.find('A' + j) == string::npos && right.find('A' + j) == string::npos)
                    coin[j].mustbenormal = true;
        }else if (judge == "up")
        {
            for (j = 0; j < left.length(); j++)
                coin[left[j] - 'A'].maybeheavy = true;
            for (j = 0; j < right.length(); j++)
                coin[right[j] - 'A'].maybelight = true;
            for (j = 0; j <= 'L' - 'A'; j++)
                if (left.find('A' + j) == string::npos && right.find('A' + j) == string::npos)
                    coin[j].mustbenormal = true;
        }
    }
}

void work()
{
    int        i, ans;

    for (i = 0; i <= 'L' - 'A'; i++)
    {
        if (coin[i].mustbenormal || (coin[i].maybeheavy && coin[i].maybelight))
            continue;
        ans = i;
    }
    if (coin[ans].maybeheavy)
        printf("%c is the counterfeit coin and it is heavy.\n", ans + 'A');
    else
        printf("%c is the counterfeit coin and it is light.\n", ans + 'A');
}

int main()
{
    //freopen("t.txt", "r", stdin);
    cin >> n;
    getchar();
    while (n--)
    {
        init();
        work();
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值