//目录

模拟,找次品硬币,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;
}

 

posted @ 2016-03-20 21:23  小草的大树梦  阅读(214)  评论(0编辑  收藏  举报