这是一道枚举题,第一次思考的时候一直陷在如何在三次称量之内找到假币,但是这道题已经明确了三次称量必定找到假币,所以呢,只需要枚举就可以了。假设一枚硬币是假的或轻或重,然后判断是否符合三个条件。需要注意的是,最后输出的是第几枚硬币是轻的还是重的,刚开始没有注意到这个细节wa了好几次。下面看下详细题目。

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
2
3
4
1 
ABCD EFGH even
ABCI EFJK up
ABIJ EFGH even

Sample Output

1
K is the counterfeit coin and it is light.

贴代码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include<iostream>
#include<string.h>
#include<vector>
#include<string>
using namespace std;

int main(){
int n;
string str;
int weight[12]={0};
vector<vector<string>> input;
while(cin>>n){
for(int f=0;f<n;f++){
input.clear();
for(int t=0;t<3;t++){
vector<string> vec;
for(int i=0;i<3;i++){
cin>>str;
vec.push_back(str);
}
input.push_back(vec);
}
bool isI = true;
string left,right;
for(int i=0;i<12;i++){
for(int v=-1;v<=1;v++){
if(v==0) continue;
isI = true;
memset(weight,0,sizeof(weight));
weight[i]=v;
for(int j=0;j<3;j++){
left=input[j][0];
right=input[j][1];
int left_w=0, right_w=0;
for(int k=0;k<left.size();k++){
left_w += weight[left[k]-65];
right_w += weight[right[k]-65];
}
if(!((left_w > right_w && input[j][2]=="up") ||
(left_w < right_w && input[j][2]=="down") ||
(left_w == right_w && input[j][2]=="even"))){
isI=false;
break;
}
}
if(isI && v==-1){
cout<<(char)('A'+i)<<" is the counterfeit coin and it is light."<<endl;
break;
}else if(isI && v==1)
{
cout<<(char)('A'+i)<<" is the counterfeit coin and it is heavy."<<endl;
break;
}
}
if(isI==true) break;
}
}
}
return 0;
}