D - Counterfeit Dollar(第二季水)

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. 

这道题如果找不到方法那么分好多种情况一种一种来分析很麻烦开始写的代码就没有找到方法,将题目样例中的情况分析完结果还有好多种情况  以下代码只考虑了样例情况  分析第二种情况时  发现太麻烦了   就停下来了
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int t;
char f1(char* a,char* b,char* c,char* d,char* e,char* f)       //a、b、c、d四个even,e、f为up||down
{
    int n1=strlen(e),n2=strlen(f);
    for(int i=0;i<n1;i++){
        if(strchr(a,e[i])==NULL&&strchr(b,e[i])==NULL&&strchr(c,e[i])==NULL&&strchr(d,e[i])==NULL){
            t=0;
            return e[i];
        }
    }
    for(int i=0;i<n2;i++){
        if(strchr(a,f[i])==NULL&&strchr(b,f[i])==NULL&&strchr(c,f[i])==NULL&&strchr(d,f[i])==NULL){
            t=1;
            return f[i];
        }
    }
}
/*char f2(char* a,char* b,char* c,char* d,char* e,char* f)        //a、b为even,其他为up||down
{

}*/
int main()
{
    int n,i;
    cin>>n;

    while(n--)
    {
        char str[15][10];
        char k;
        for(i=0;i<3;i++)cin>>str[i];
        for(;i<6;i++)cin>>str[i];
        for(;i<9;i++)cin>>str[i];
        //for(i=0;i<9;i++)cout<<i<<" "<<str[i]<<endl;
        if(str[2][0]==‘e‘&&str[5][0]==‘e‘&&(str[8][0]==‘u‘||str[8][0]==‘d‘)){
            k=f1(str[0],str[1],str[3],str[4],str[6],str[7]);
            cout<<k<<" is the counterfeit coin and it is ";
            if(str[8][0]==‘u‘){
                if(t==0)cout<<"heavy. ";
                else cout<<"light. ";
            }
            else{
                if(t==0)cout<<"light. ";
                else cout<<"heavy. ";
            }
        }
        else if(str[8][0]=‘e‘&&str[2][0]==‘e‘&&(str[5][0]==‘u‘||str[5][0]==‘d‘)){
            k=f1(str[0],str[1],str[6],str[7],str[3],str[4]);
            cout<<k<<" is the counterfeit coin and it is ";
            if(str[5][0]==‘u‘){
                if(t==0)cout<<"heavy. ";
                else cout<<"light. ";
            }
            else{
                if(t==0)cout<<"light. ";
                else cout<<"heavy. ";
            }
        }
        else if(str[5][0]==‘e‘&&str[8][0]==‘e‘&&(str[2][0]==‘u‘||str[2][0]==‘d‘)){
            k=f1(str[3],str[4],str[6],str[7],str[0],str[1]);
            cout<<k<<" is the counterfeit coin and it is ";
            if(str[2][0]==‘u‘){
                if(t==0)cout<<"heavy. ";
                else cout<<"light. ";
            }
            else{
                if(t==0)cout<<"light. ";
                else cout<<"heavy. ";
            }
        }
    /*    else if(str[2][0]==‘e‘&&str[5][0]!=‘e‘&&str[8][0]!=‘e‘){
        }
        else if(str[5][0]==‘e‘&&str[2][0]!=‘e‘&&str[8][0]!=‘e‘){
        }
        else if(str[8][0]==‘e‘&&str[5][0]!=‘e‘&&str[2][0]!=‘e‘){
        }
        */
    }
    //system("pause");
    return 0;
}

找找方法  换一种简单的方法来做

给所有银币赋值1

从A开始给银币赋值0或 2   天平两端相加判断是否符合even up down的条件 若符合 则得出结果

#include<iostream>
#include<string>
using namespace std;
int s[15];
int f(string a,string b,int n)
{
    int p=0,q=0;
    for(int j=0;j<n;j++){
        p+=s[a[j]-‘A‘];
        q+=s[b[j]-‘A‘];
    }
    if(p==q)return 0;
    if(p>q)return 1;
    else return -1;
}
int main()
{
    int n,i;
    cin>>n;
    while(n--)
    {
        for(i=0;i<15;i++)s[i]=1;
        string str[15];
        int k;
        bool flag=false;
        for(i=0;i<3;i++)cin>>str[i];
        for(;i<6;i++)cin>>str[i];
        for(;i<9;i++)cin>>str[i];
        int n1=str[0].length(),n2=str[3].length(),n3=str[6].length();
        for(i=0;i<12;i++){
            s[i]=0;
            k=f(str[0],str[1],n1);
            if(!((k==0&&str[2][0]==‘e‘)||(k==1&&str[2][0]==‘u‘)||(k==-1&&str[2][0]==‘d‘))){
                s[i]=1;
                continue;
            }
            k=f(str[3],str[4],n2);
            if(!((k==0&&str[5][0]==‘e‘)||(k==1&&str[5][0]==‘u‘)||(k==-1&&str[5][0]==‘d‘))){
                s[i]=1;
                continue;
            }
            k=f(str[6],str[7],n3);
            if((k==0&&str[8][0]==‘e‘)||(k==1&&str[8][0]==‘u‘)||(k==-1&&str[8][0]==‘d‘)){
                flag=true;
                cout<<(char)(i+‘A‘)<<" is the counterfeit coin and it is light. "<<endl;
            }
            s[i]=1;
        }
        if(!flag){
            for(i=0;i<12;i++){
                s[i]=2;
                k=f(str[0],str[1],n1);
                if(!((k==0&&str[2][0]==‘e‘)||(k==1&&str[2][0]==‘u‘)||(k==-1&&str[2][0]==‘d‘))){
                s[i]=1;
                continue;
            }
                k=f(str[3],str[4],n2);
                if(!((k==0&&str[5][0]==‘e‘)||(k==1&&str[5][0]==‘u‘)||(k==-1&&str[5][0]==‘d‘))){
                s[i]=1;
                continue;
            }
                k=f(str[6],str[7],n3);
                if((k==0&&str[8][0]==‘e‘)||(k==1&&str[8][0]==‘u‘)||(k==-1&&str[8][0]==‘d‘)){
                    cout<<(char)(i+‘A‘)<<" is the counterfeit coin and it is heavy. "<<endl;
                }
                s[i]=1;
            }
        }
    }
    //system("pause");
    return 0;
}
时间: 2024-07-29 13:27:40

D - Counterfeit Dollar(第二季水)的相关文章

F - The Fun Number System(第二季水)

Description In a k bit 2's complement number, where the bits are indexed from 0 to k-1, the weight of the most significant bit (i.e., in position k-1), is -2^(k-1), and the weight of a bit in any position i (0 ≤ i < k-1) is 2^i. For example, a 3 bit

I - Long Distance Racing(第二季水)

Description Bessie is training for her next race by running on a path that includes hills so that she will be prepared for any terrain. She has planned a straight path and wants to run as far as she can -- but she must be back to the farm within M se

S - 骨牌铺方格(第二季水)

Description 在2×n的一个长方形方格中,用一个1× 2的骨牌铺满方格,输入n ,输出铺放方案的总数.         例如n=3时,为2× 3方格,骨牌的铺放方案有三种,如下图:         Input 输入数据由多行组成,每行包含一个整数n,表示该测试实例的长方形方格的规格是2×n (0<n<=50). Output 对于每个测试实例,请输出铺放方案的总数,每个实例的输出占一行. Sample Input 1 3 2 Sample Output 1 3 2 水题,同上一道小蜜

V - 不容易系列之(4)――考新郎(第二季水)

Description 国庆期间,省城HZ刚刚举行了一场盛大的集体婚礼,为了使婚礼进行的丰富一些,司仪临时想出了有一个有意思的节目,叫做"考新郎",具体的操作是这样的:         首先,给每位新娘打扮得几乎一模一样,并盖上大大的红盖头随机坐成一排;         然后,让各位新郎寻找自己的新娘.每人只准找一个,并且不允许多人找一个.         最后,揭开盖头,如果找错了对象就要当众跪搓衣板...         看来做新郎也不是容易的事情...         假设一共有

W - Bitset(第二季水)

Description Give you a number on base ten,you should output it on base two.(0 < n < 1000) Input For each case there is a postive number n on base ten, end of file. Output For each case output a number on base two. Sample Input 1 2 3 Sample Output 1

J - A + B Problem II(第二季水)

Description I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B. Input The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, eac

A - 高精度(大数)N次方(第二季水)

Description Problems involving the computation of exact values of very large magnitude and precision are common. For example, the computation of the national debt is a taxing experience for many computer systems.         This problem requires that yo

P - A + B(第二季水)

Description 读入两个小于100的正整数A和B,计算A+B. 需要注意的是:A和B的每一位数字由对应的英文单词给出. Input 测试输入包含若干测试用例,每个测试用例占一行,格式为"A + B =",相邻两字符串有一个空格间隔.当A和B同时为0时输入结束,相应的结果不要输出. Output 对每个测试用例输出1行,即A+B的值. Sample Input one + two = three four + five six = zero seven + eight nine

N - Robot Motion(第二季水)

Description A robot has been programmed to follow the instructions in its path. Instructions for the next direction the robot is to move are laid down in a grid. The possible instructions are N north (up the page)         S south (down the page)