POJ 1013

Counterfeit Dollar

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 51205   Accepted: 16025

Description

Sally Jones has a dozen Voyageur silver dollars. However, only eleven of the coins are true silver dollars; one coin is counterfeit even though its color and size make it indistinguishable from the real silver dollars. The counterfeit coin has a different weight from the other coins but Sally does not know if it is heavier or lighter than the real coins. 
Happily, Sally has a friend who loans her a very accurate balance scale. The friend will permit Sally three weighings to find the counterfeit coin. For instance, if Sally weighs two coins against each other and the scales balance then she knows these two coins are true. Now if Sally weighs 
one of the true coins against a third coin and the scales do not balance then Sally knows the third coin is counterfeit and she can tell whether it is light or heavy depending on whether the balance on which it is placed goes up or down, respectively. 
By choosing her weighings carefully, Sally is able to ensure that she will find the counterfeit coin with exactly three weighings.

Input

The first line of input is an integer n (n > 0) specifying the number of cases to follow. Each case consists of three lines of input, one for each weighing. Sally has identified each of the coins with the letters A--L. Information on a weighing will be given by two strings of letters and then one of the words ``up'', ``down'', or ``even''. The first string of letters will represent the coins on the left balance; the second string, the coins on the right balance. (Sally will always place the same number of coins on the right balance as on the left balance.) The word in the third position will tell whether the right side of the balance goes up, down, or remains even.

Output

For each case, the output will identify the counterfeit coin by its letter and tell whether it is heavy or light. The solution will always be uniquely determined.

Sample Input

1 
ABCD EFGH even
ABCI EFJK up
ABIJ EFGH even

Sample Output

K is the counterfeit coin and it is light. 

Source

East Central North America 1998

 

大致题意:

有一打(12枚)硬币,其中有且仅有1枚假币,11枚真币

用A~L作为各个硬币的代号

假币可能比真币略轻,也可能略重

现在利用天枰,根据Input输入的3次称量,找出假币,并输出假币是轻还是重。

解题思路:

1、  有且仅有1枚假币

2、  假币相对于真币的重量,可能轻可能重

3、  只称量3次,且称量3次恰好且必能找到假币

4、  每次称量时天枰两边的硬币数目一样

5、  选取哪些硬币称量由input决定

因此读取三次输入后,利用两个数组tr[12]以及flag[12],两者初始化都为0

当出现even时,两边所有字母对应的tr[i]变为1,表明这肯定为真硬币

当出现up或down时,

                            若up,则对目前还不知为真币的硬币,左边盘中的flag[i]++,右边的flag[i]--

                            若down,则对目前还不知为真币的硬币,左边盘中的flag[i]--,右边的flag[i]++

最后flag中绝对值最大的那个且tr[i]为0的,就是最值得怀疑的那个假币,根据flag正负判断是轻还是重

#include <iostream>
#include<cmath>
using namespace std;
void even(string a,string b,int flag[12],int tr[12])
{
    for(int i=0;i<a.size();i++)
    {
        tr[a[i]-'A']=1; 
        flag[a[i]-'A']=0;
    }
    for(int i=0;i<b.size();i++)
    {
        tr[b[i]-'A']=1;
        flag[a[i]-'A']=0;
    }
}
void up(string a,string b,int flag[12],int tr[12])
{
    for(int i=0;i<a.size();i++)//a这边有重的 
    {
        if(tr[a[i]-'A']!=1)//即不在确定列表时 
        {
         flag[a[i]-'A']++;//为其怀疑度加一 
        }
    }
    
    for(int i=0;i<b.size();i++)
    {
        if(tr[b[i]-'A']!=1)
        {
         flag[b[i]-'A']--;
        }
    }
}
void down(string a,string b,int flag[12],int tr[12])
{
    for(int i=0;i<a.size();i++)
    {
        if(tr[a[i]-'A']!=1)
        {
         flag[a[i]-'A']--;
        }
    }
    
    for(int i=0;i<b.size();i++)
    {
        if(tr[b[i]-'A']!=1)
        {
         flag[b[i]-'A']++;
        }
    }
}
int main()
{
 int n;
 cin>>n;
 while(n--)
 {
     int flag[12]={0};
     int tr[12]={0}; 
     for(int i=0;i<3;i++)
     {
         string a,b,c;
         cin>>a>>b>>c;
         if(c=="even")
         {
           even(a,b,flag,tr);
        }
        if(c=="up")
        {
            up(a,b,flag,tr);
        }
        if(c=="down")
        {
            down(a,b,flag,tr);
        }
     }
     
       int max=0;
       char ans;
       for(int i=0;i<12;i++)
       {
           if(abs(flag[i])>=max)
         {
            max=abs(flag[i]);
            ans='A'+i;
         }    
       }
         if(flag[ans-'A']>0)
         cout<<ans<<" is the counterfeit coin and it is heavy."<<endl;
         if(flag[ans-'A']<0)
         cout<<ans<<" is the counterfeit coin and it is light."<<endl;
    }
}

上面的编译出错了,同时有点麻烦,改了一下

原因:

使用了abs()求绝对值,但是头文件是cmath

c语言书本上说,数学函数除了求整数的绝对值函数abs()之外<abs() 定义在stdlib.h中>,其余的函数都在头文件 math.h 中定义,包括对浮点数求绝对值的函数fabs()。
c++中,包含的相应的头文件为,原则是前面加c,同时去掉.h 。
例如:
#include <cstdlib>对应        #include <stdlib.h>
#include <cmath>对应        #include <math.h>
于是,在c++里        
                使用abs()就用 #include <cstdlib>
                使用fabs()就用 #include <cmath> 

#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
void even(string a,string b,int flag[12],int tr[12])
{
    for(int i=0;i<a.size();i++)
    
        tr[a[i]-'A']=1; 
    
    for(int i=0;i<b.size();i++)
    
        tr[b[i]-'A']=1;
    
}
void up(string a,string b,int flag[12],int tr[12])
{
    for(int i=0;i<a.size();i++)//a这边有重的         
         flag[a[i]-'A']++;//为其怀疑度加一 
    
    for(int i=0;i<b.size();i++)
         flag[b[i]-'A']--;

}
void down(string a,string b,int flag[12],int tr[12])
{    
    for(int i=0;i<a.size();i++)//a这边有重的         
         flag[a[i]-'A']--;//为其怀疑度加一 
    for(int i=0;i<b.size();i++)
         flag[b[i]-'A']++;
}
int main()
{
 int n;
 cin>>n;
 while(n--)
 {
     int flag[12]={0};
     int tr[12]={0}; 
     for(int i=0;i<3;i++)
     {
         string a,b,c;
         cin>>a>>b>>c;
         if(c=="even")
         {
           even(a,b,flag,tr);
        }
        if(c=="up")
        {
            up(a,b,flag,tr);
        }
        if(c=="down")
        {
            down(a,b,flag,tr);
        }
     }
     
       int max=0;
       char ans;
       for(int i=0;i<12;i++)
       {
           if(abs(flag[i])>=max&&tr[i]==0)
         {
            max=abs(flag[i]);
            ans='A'+i;
         }    
       }
         if(flag[ans-'A']>0)
         cout<<ans<<" is the counterfeit coin and it is heavy."<<endl; 
         if(flag[ans-'A']<0)
         cout<<ans<<" is the counterfeit coin and it is light."<<endl;
    }
    return 0; 
}

 

posted @ 2018-07-24 16:23  fantastic123  阅读(150)  评论(0编辑  收藏  举报