模拟,找次品硬币,Counterfeit Dollar(POJ 1013)

题目链接:http://poj.org/problem?id=1013

解题报告:

1、由于次品的重量不清楚,用time['L'+1]来记录各个字母被怀疑的次数。为负数则轻,为正数则重。

2、用zero['L'+1]记录当天平结果是even时,硬币绝对是真,true;

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        char left[3][6],right[3][6],result[3][6];

        int time['L'+1]= {0}; ///标记各个字母被怀疑的次数;
        bool zero['L'+1]= {false}; ///标记字母绝对是真币;

        for(int k=0; k<3; k++)
            cin>>left[k]>>right[k]>>result[k];

        for(int i=0; i<3; i++) ///3次判断
        {
            ///检查天平状态
            switch(result[i][0])
            {
            case 'e':///天平平衡
            {
                for(int j=0; left[i][j]!='\0'; j++)
                {
                    zero[left[i][j]]=true;

                    zero[right[i][j]]=true;
                }
                break;
            }
            case 'u':///天平右边轻些
            {
                for(int j=0; left[i][j]!='\0'; j++)
                {
                    time[left[i][j]]++;

                    time[right[i][j]]--;
                }
                break;
            }
            case 'd':///天平右边重些
            {
                for(int j=0; left[i][j]!='\0'; j++)
                {
                    time[left[i][j]]--;

                    time[right[i][j]]++;
                }
                break;
            }
            }
        }


        int Max=-1;///查找被怀疑程度最高的硬币
        char alpha;
        for(int j='A';j<='L';j++)
        {
            if(zero[j])
                continue;

            if(Max<=abs(time[j]))
            {
                Max=abs(time[j]);
                alpha=j;
            }
        }
        cout<<alpha<<" is the counterfeit coin and it is ";
        if(time[alpha]>0)
            cout<<"heavy.\n";
        else cout<<"light.\n";
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/TreeDream/p/5299714.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值