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;
}

 

 

你可能感兴趣的:(D - Counterfeit Dollar(第二季水))