标签:
Description
Input
Output
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; }
标签:
原文地址:http://www.cnblogs.com/farewell-farewell/p/5202070.html