pku1013

来源:互联网 发布:安信车辆软件 编辑:程序博客网 时间:2024/06/07 23:00

这道题目的关键在于:输入会决定唯一的结果,所以可以用枚举来找到不相等的那个。

#include<iostream>#include<algorithm>#include<string>using namespace std;string Left[3],Right[3],result[3];string a ="ABCDEFGHIJKL";bool isnotlight(int num){for(int i =0;i<3;i++){if(result[i] == "even"){if(Left[i].find(a[num]) != string::npos && Right[i].find(a[num]) == string::npos)return true;if(Right[i].find(a[num]) != string::npos&& Left[i].find(a[num]) == string::npos)return true;}else if(result[i] == "up"){if(Right[i].find(a[num]) == string::npos)return true;}else if(result[i] == "down"){if(Left[i].find(a[num]) == string::npos)return true;}}return false;}bool isnotheavy(int num){for(int i =0;i<3;i++){if(result[i] == "even"){if(Left[i].find(a[num]) != string::npos && Right[i].find(a[num]) == string::npos)return true;if(Right[i].find(a[num]) != string::npos&& Left[i].find(a[num]) == string::npos)return true;}else if(result[i] == "up"){if(Left[i].find(a[num]) == string::npos)return true;}else if(result[i] == "down"){if(Right[i].find(a[num]) == string::npos)return true;}}return false;}int main(){int n;cin>>n;while(n){for(int i =0;i<3;++i)cin>>Left[i]>>Right[i]>>result[i];for(int i =0;i<12;++i){if(!isnotlight(i)){cout<<a[i]<<" "<<"is the counterfeit coin and it is light."<<endl;break;}if(!isnotheavy(i)){cout<<a[i]<<" "<<"is the counterfeit coin and it is heavy."<<endl;break;}}--n;}system("PAUSE");return 0;}
另外一种思路,根据even把所有相等的先排除,对不相等的通过在不平衡端次数进行统计,唯一的那个一定会是绝对值最大的。
#include<iostream>#include<string>#include<string.h>using namespace std;char Left[6],Right[6],result[6];string a ="ABCDEFGHIJKL";bool isequal[12];int flag[12];int main(){int n;cin>>n;while(n){memset(isequal,0,sizeof(isequal));memset(flag,0,sizeof(flag));for(int i =0;i<3;++i){cin>>Left>>Right>>result;if(strcmp(result,"even") == 0){for(unsigned j =0;j<strlen(Left);j++){if(isequal[Left[j]-'A'] == false)isequal[Left[j]-'A'] = true;}for(unsigned j =0;j<strlen(Right);j++){if(isequal[Right[j]-'A'] == false)isequal[Right[j]-'A'] = true;}}else if(strcmp(result,"up") == 0){for(unsigned j =0;j<strlen(Left);j++){flag[Left[j]-'A']++;}for(unsigned j =0;j<strlen(Right);j++){flag[Right[j]-'A']--;}}else if(strcmp(result,"down") == 0){for(unsigned j =0;j<strlen(Left);j++){flag[Left[j]-'A']--;}for(unsigned j =0;j<strlen(Right);j++){flag[Right[j]-'A']++;}}}int max = -1;int k= 0;for(int i=0;i<12;++i){if(isequal[i] == true)continue;if(abs(flag[i]) > max){max = abs(flag[i]);k = i;}}if(flag[k]< 0){cout<<a[k]<<" is the counterfeit coin and it is light."<<endl;}else{cout<<a[k]<<" is the counterfeit coin and it is heavy."<<endl;}--n;}return 0;}

0 0