pku1013

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值