天天看点

FZU 1003 Counterfeit Dollar

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

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.

简单枚举

#include<stack>
#include<queue>
#include<cmath>
#include<cstdio>
#include<vector>
#include<string>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn=1000005;
int T;
char ss[2][100]={"light","heavy"};
char s[3][3][10];

bool check(char a,int b)
{
  for (int i=0;i<3;i++)
  {
    int ans[2]={0,0};
    for (int j=0;j<2;j++)
    {
      for (int k=0;s[i][j][k];k++)
        if (s[i][j][k]==a) ans[j]+=b; 
    }
    if (ans[0]==ans[1]&&s[i][2][0]!='e') return false;
    if (ans[0]<ans[1]&&s[i][2][0]!='d') return false;
    if (ans[0]>ans[1]&&s[i][2][0]!='u') return false;
  }
  return true;
}

int main()
{
  scanf("%d",&T);
  while (T--)
  {
    for (int i=0;i<3;i++) 
      for (int j=0;j<3;j++) scanf("%s",s[i][j]);
    for (char i='A';i<='L';i++)
      for (int j=-1;j<2;j+=2)
        if (check(i,j)) printf("%c is the counterfeit coin and it is %s.\n",i,ss[max(j,0)]);
  }
  return 0;
}