摘要:枚举所有情况,找出符合条件的硬币。
...
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.
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
Sample Input
Sample Output
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.
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.
1 ABCD EFGH even ABCI EFJK up ABIJ EFGH even
K is the counterfeit coin and it is light.
题目理解:
通过条件计算找到正确的硬币可能操作比较复杂,枚举检验可能更方便。将有硬币的一边当做1,无为0;每次只是判断10,01,11,00;情况是否符合平衡条件。
日期:
2017 07 15
附加:
用了strstr(),但还有个strchr()函数;
代码:
1
2
3
4
5
6
using namespace std;
7
8
9
10
char coin[] = "ABCDEFGHIJKL";
11
12
struct W{
13
char WL[3][max];
14
char WR[3][max];
15
char state[3][max];
16
char s[3];
17
};
18
19
W w;
20
21
void getin(){
22
23
for(int i=0 ;i<3;i++){
24
scanf("%s%s%s",w.WL[i],w.WR[i],w.state[i]);
25
w.s[i] = w.state[i][0];
26
//cout<<w.WL[i]<<endl<<w.WR[i]<<endl<<w.s[i]<<endl;
27
}
28
}
29
30
bool mtry(int j){
31
char c[] = {coin[j],'\0'};//which coin
32
char bl;//blance
33
bool l=false ,r=false,s_h=true,s_l=true;//state
34
35
for(int i =0 ;i< 3;i++){
36
l=false ,r=false;
37
if(strstr(w.WR[i],c)) r = true;
38
if(strstr(w.WL[i],c)) l = true;
39
bl = w.s[i];
40
41
if((l&r)||(!l&!r)){
42
if(bl=='e')continue;
43
else return false;
44
}else{
45
if(bl=='e') return false;
46
47
if(!l&r){// 0 1
48
if(bl=='u') s_h = false;
49
else s_l = false;
50
}
51
if(l&!r){// 1 0
52
if(bl=='u') s_l = false;
53
else s_h = false;
54
}
55
}
56
57
}
58
if(s_l||s_h){
59
s_h?printf("%c is the counterfeit coin and it is heavy.\n",coin[j])
60
:printf("%c is the counterfeit coin and it is light.\n",coin[j]);
61
62
return true;
63
}else return false;
64
65
}
66
67
void solve(){
68
getin();
69
for(int i=0; i<max; i++) if(mtry(i)) break;
70
}
71
72
int main(){
73
int t;scanf("%d",&t);
74
while(t--)solve();
75
return 0;
76
}