poj1013找出问题硬币

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




题目大意有十几个硬币。 然而,只有十一个硬币是真正的银元;  即使它的颜色和大小与真正的银元无法区分,一枚硬币也是假币。 假币与其他币有不同的重量,但是不知道它是重还是轻。 



输入:第一行输入是一个组数。每种情况由三行输入组成,每次称重一行。用字母A - L标识了每个硬币。关于称量的信息将由两个字母串然后是“up”,“down”或“even”中的一个来给出。第一串字母代表左边平衡的硬币; 第二个字符串,右边的硬币平衡。(Sally总是会在左边的余额上放置相同数量的硬币)。第三个位置的单词表示天平的右侧是上升,下降还是保持平衡。左右两边硬币个数相等。



对于每一种情况,输出都将通过字母识别伪币,并判断它是重还是轻。 解决方案将始终是唯一确定的。



Sample Input

1 
ABCD EFGH even 
ABCI EFJK up 
ABIJ EFGH even 

Sample Output

K is the counterfeit coin and it is light. 


使用结构体保存每个硬币的信息。
class coin{
public:
    bool exist;
    int weight;//0为真币
    int really;//1为确定,0为不确定
    coin()
    {
        exist=0;
        weight=0;
        really=0;
    }
};
为even的时候,表示左右两边都是真的硬币。
为up的时候,有两种情况:左边有一个重的假币或者右边有一个轻的假币。
为down的时候,有两种情况:左边有一个轻的假币或者右边有一个重的假币。
信息录入成功后,寻找偏离重量0最大的那个硬币,就是假币。重量大于0就是重,小于就算轻。

#include <iostream>
#include <cstring>
#include <math.h>
using namespace std;
class coin{
public:
    bool exist;
    int weight;//0为真币
    int really;//1为确定,0为不确定
    coin()
    {
        exist=0;
        weight=0;
        really=0;
    }
};
int main()
{
    int n;
    cin>>n;
    while(n--)
    {
        coin a[13];
        int t=3;
        while(t--)
        {
            string x,y,z;
            cin>>x>>y>>z;
            for(int i=0;i<x.length();i++)
                a[x[i]-'A'+1].exist=1;
            for(int i=0;i<y.length();i++)
                a[y[i]-'A'+1].exist=1;
            if(z=="even")
            {
                for(int i=0;i<x.length();i++)
                {
                    a[x[i]-'A'+1].really=1;
                    a[x[i]-'A'+1].weight=0;
                }
                for(int i=0;i<y.length();i++)
                {
                    a[y[i]-'A'+1].really=1;
                    a[y[i]-'A'+1].weight=0;
                }
            }
            else if(z=="up")
            {
                for(int i=0;i<x.length();i++)
                {

                    if(a[x[i]-'A'+1].really==1)
                        continue;
                    a[x[i]-'A'+1].weight++;
                    a[x[i]-'A'+1].really=0;
                }
                for(int i=0;i<y.length();i++)
                {
                    if(a[y[i]-'A'+1].really==1)
                        continue;
                    a[y[i]-'A'+1].weight--;
                    a[y[i]-'A'+1].really=0;
                }
            }
            else if(z=="down")
            {
                 for(int i=0;i<x.length();i++)
                {
                    if(a[x[i]-'A'+1].really==1)
                        continue;
                    a[x[i]-'A'+1].weight--;
                    a[x[i]-'A'+1].really=0;
                }
                for(int i=0;i<y.length();i++)
                {
                    if(a[y[i]-'A'+1].really==1)
                        continue;
                    a[y[i]-'A'+1].weight++;
                    a[y[i]-'A'+1].really=0;
                }
            }
        }
        int x,y;
        x=y=0;
        for(int i=1;i<=12;i++)
        {
            if(a[i].really==1) continue;
            if(fabs(a[i].weight)>fabs(x))
            {
                x=a[i].weight;
                y=i;
            }
        }
        cout<<char('A'+y-1)<<" is the counterfeit coin and it is ";
        if(x<0)cout<<"light."<<endl;
        else cout<<"heavy."<<endl;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值