Poj 2692:假币问题

2692:假币问题

总时间限制: 
1000ms 
内存限制: 
65536kB
描述
赛利有12枚银币。其中有11枚真币和1枚假币。假币看起来和真币没有区别,但是重量不同。但赛利不知道假币比真币轻还是重。于是他向朋友借了一架天平。朋友希望赛利称三次就能找出假币并且确定假币是轻是重。例如:如果赛利用天平称两枚硬币,发现天平平衡,说明两枚都是真的。如果赛利用一枚真币与另一枚银币比较,发现它比真币轻或重,说明它是假币。经过精心安排每次的称量,赛利保证在称三次后确定假币。
输入
第一行有一个数字n,表示有n组测试用例。
对于每组测试用例:
输入有三行,每行表示一次称量的结果。赛利事先将银币标号为A-L。每次称量的结果用三个以空格隔开的字符串表示:天平左边放置的硬币 天平右边放置的硬币 平衡状态。其中平衡状态用``up'', ``down'', 或 ``even''表示, 分别为右端高、右端低和平衡。天平左右的硬币数总是相等的。
输出
输出哪一个标号的银币是假币,并说明它比真币轻还是重(heavy or light)。
样例输入
1
ABCD EFGH even 
ABCI EFJK up 
ABIJ EFGH even 
样例输出

K is the counterfeit coin and it is light.


6
ABCD EFGH even
IJ KA up
JK AB up
ABCD EFGH even
IJ KA up
JK AB down
ABCD EFGH even
IJ KA up
JK AB even
ABCD EFGH even
IJ KA down
JK AB up
ABCD EFGH even
IJ KA down
JK AB down
ABCD EFGH even
IJ KA down
JK AB even

 

!!!!

输出结果:

J is the counterfeit coin and it is heavy.
K is the counterfeit coin and it is light.
I is the counterfeit coin and it is heavy.
K is the counterfeit coin and it is heavy.
J is the counterfeit coin and it is light.
I is the counterfeit coin and it is light.


#include "string"
#include "string.h"
#include "stdio.h" 
#include "iostream"
using namespace std;
#define Max 20
int N;
string Str[3];
string Left[3],Right[3];
int  A[12];
bool IsHevay(char p)
{
      for(int i=0;i<3;i++)
	  {
        if(Str[i]=="even")
		continue;	
		else
		{ 
			if(Str[i]=="up")
			{  
				if(Left[i].find(p)==string::npos)
				{
					return false;
				}
			}
			if(Str[i]=="down")
			{
				if(Right[i].find(p)==string::npos)
				{
					return false;
				}
			}
		}
	  }
	  return true;
}

bool IsLight(char p)
{
	for(int i=0;i<3;i++)
	{
        if(Str[i]=="even")
		continue;
		if(Str[i]=="up")
		{
			if(Right[i].find(p)==string::npos)
			{
				return false;
			}
		}
		if(Str[i]=="down")
		{
			if(Left[i].find(p)==string::npos)
			{
				return false;
			}
		}
	}
    return true;
}

int main( )
{ 
    //freopen("1.txt","r",stdin);
	int i;
	cin>>N;	
	while(N--)
	{
		memset(A,0,sizeof(A));
		for(i=0;i<3;i++)
		{
			cin>>Left[i]>>Right[i]>>Str[i];		
			if(Str[i]=="even")
			{
				const char *s=Left[i].c_str();
				while(*s!='\0')
				{
					int x=*s-'A';
					A[x]=1;
					s++;
				}
				const char *p=Right[i].c_str();
				while(*p!='\0')
				{  
					int x=*p-'A';
					A[x]=1;
					p++;
				}
			}
		}
		for(i=0;i<12;i++)
		{
			if(A[i]) continue;
			else
			{
				char T='A'+i;

				if(IsHevay(T))
				{
					cout<<T<<" is the counterfeit coin and it is heavy. "<<endl;
					break;
				}
				if(IsLight(T))
				{					
					cout<<T<<" is the counterfeit coin and it is light. "<<endl;
					break;
				}
			}
		}


	}
	
	
	return 0;
}


猜你喜欢

转载自blog.csdn.net/qq_34125999/article/details/51464029