POJ 1013 Counterfeit Dollar

Counterfeit Dollar
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 41162   Accepted: 13123

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. 

题目大意:有12个硬币,其中有且只有一个假币,其实重量较真币或轻或重未知,现通过3次称量确定哪个是假币,并判断是较真币是轻了还是重了。3次称量方式有输入确定。
即:
left   right  state("even","up","down")
even是平衡,up是左重,down是右重


模拟肯定是难以实现的,我一开始的想法:首先,even时,天平两边的硬币全部是真币,这很明显,假如我用一个数组vis来表示的话,vis=-1是初始化,vis[i]=0表示i确定是真的,vis[i]=1表示i可能轻,vis[i]=2表示i可能重,如果vis[i]以前被标记过为1,而现在又出现它在重的那一边,则说明它是真的;反之也是这样。 不过按这个做法交上去是错的。应该还是不严谨。

然后就看网上的题解,题解很简单,就是记录每个硬币的被怀疑程度,最后被怀疑程度最深的那个就是假币。(其实最后即便是每个真币的被怀疑程度也有可能不一样,不过我们只需要找程度最深的那个即可)。

#include <stdio.h>
#include <string.h>
#include <math.h>
#define eps 1e-9

int main()
{
	int i,j,n,l,vis[20],v,m;
	char s1[20],s2[20],s[20];
	scanf("%d",&n);
	while(n--)
	{
		memset(vis,0,sizeof(vis));
		for(i=0;i<3;i++)
		{
			scanf("%s%s%s",s1,s2,s);
			l=strlen(s1);
			if(s[0]=='e')
			{
				for(j=0;j<l;j++)
					vis[s1[j]-'A']=vis[s2[j]-'A']=10;
			}
			else if(s[0]=='d')	//后面重
			{
				for(j=0;j<l;j++)
				{
					if(vis[s1[j]-'A']!=10)
						vis[s1[j]-'A']--;
					if(vis[s2[j]-'A']!=10)
						vis[s2[j]-'A']++;
				}
			}
			else 
				for(j=0;j<l;j++)
				{
					if(vis[s1[j]-'A']!=10)
						vis[s1[j]-'A']++;
					if(vis[s2[j]-'A']!=10)
						vis[s2[j]-'A']--;
				}
		}
		v=-1;m=-1;
		for(i=0;i<12;i++)
			if(vis[i]!=10 && v<abs(vis[i]))
				m=i,v=abs(vis[i]);
		printf("%c is the counterfeit coin and it is ",'A'+m);
		if(vis[m]<0)
			printf("light.\n");
		else
			printf("heavy.\n");
	}
	return 0;
}


你可能感兴趣的:(poj)