http://acm.pku.edu.cn/JudgeOnline/problem?id=1013
参考了http://www.skywind.name/blog/?p=143
顺便介绍一个网站 http://www.curiouser.co.uk/frames/creframe.html?http://www.curiouser.co.uk/puzzles/12bsolutions.htm, 里面有12balls问题的各种奇怪解法.
开始一直WA, 原来是light和heavy后面没有加句号"."!!!!
2 #include < string >
3 #include < fstream >
4 #include < memory.h >
5 using namespace std;
6
7 int main()
8 {
9 int c[12][3];
10 int res[3];
11 int n, i, j;
12
13 cin>>n;
14 while(n--) {
15 memset(c, 0, sizeof(c));
16 memset(res, 0, sizeof(res));
17 string s1, s2, s3;
18 for(i=0; i<3; i++){
19 cin>>s1>>s2>>s3;
20 for(j=0; j<s1.size(); j++) {
21 c[s1[j]-'A'][i] = 1;
22 c[s2[j]-'A'][i] = -1;
23 }
24 if(s3=="even"){
25 res[i] = 0;
26 }
27 else if(s3=="up"){
28 res[i] = 1;
29 }
30 else {
31 res[i] = -1;
32 }
33 }
34 for(i=0; i<13; i++) {
35 if(c[i][0]==res[0] && c[i][1]==res[1] && c[i][2]==res[2]) {
36 cout<<char('A'+i)<<" is the counterfeit coin and it is heavy."<<endl;;
37 break;
38 }
39 else if(c[i][0]==-res[0] && c[i][1]==-res[1] && c[i][2]==-res[2]) {
40 cout<<char('A'+i)<<" is the counterfeit coin and it is light."<<endl;;
41 break;
42 }
43 }
44 }
45 return 0;
46}
下面是枚举的方法:
2 {
3 int c[12];
4 int res[3];
5 int n, i, j;
6 cin>>n;
7 while(n--) {
8 memset(c, 0, sizeof(c));
9 memset(res, 0, sizeof(res));
10 string s1[3], s2[3], s3[3];
11 for(i=0; i<3; i++){
12 cin>>s1[i]>>s2[i]>>s3[i];
13 }
14 for(i=0; i<12; i++) {
15 memset(c, 0, sizeof(c));
16 c[i] = 1;
17 int left, right;
18 for(j=0; j<3; j++) {
19 left = 0; right = 0;
20 for(int k=0; k<s1[j].size(); k++) {
21 left += c[s1[j][k]-'A'];
22 right += c[s2[j][k]-'A'];
23 }
24 if( (left>right && s3[j]!="up") || (left<right && s3[j]!="down")
25 || (left==right && s3[j]!="even") ){
26 break;
27 }
28 }
29 if(j==3) {
30 cout<<char('A'+i)<<" is the counterfeit coin and it is heavy."<<endl;
31 }
32 c[i] = -1;
33 for(j=0; j<3; j++) {
34 left = 0; right = 0;
35 for(int k=0; k<s1[j].size(); k++) {
36 left += c[s1[j][k]-'A'];
37 right += c[s2[j][k]-'A'];
38 }
39 if( (left>right && s3[j]!="up") || (left<right && s3[j]!="down")
40 || (left==right && s3[j]!="even") ){
41 break;
42 }
43 }
44 if(j==3) {
45 cout<<char('A'+i)<<" is the counterfeit coin and it is light."<<endl;
46 }
47 }
48 }
49 return 0;
50}
1#include <iostream>
2#include <string>
3#include <fstream>
4#include <memory.h>
5using namespace std;
6
7int main()
8{
9 int c[12][3];
10 int res[3];
11 int n, i, j;
12
13 cin>>n;
14 while(n--) {
15 memset(c, 0, sizeof(c));
16 memset(res, 0, sizeof(res));
17 string s1, s2, s3;
18 for(i=0; i<3; i++){
19 cin>>s1>>s2>>s3;
20 for(j=0; j<s1.size(); j++) {
21 c[s1[j]-'A'][i] = 1;
22 c[s2[j]-'A'][i] = -1;
23 }
24 if(s3=="even"){
25 res[i] = 0;
26 }
27 else if(s3=="up"){
28 res[i] = 1;
29 }
30 else {
31 res[i] = -1;
32 }
33 }
34 for(i=0; i<13; i++) {
35 if(c[i][0]==res[0] && c[i][1]==res[1] && c[i][2]==res[2]) {
36 cout<<char('A'+i)<<" is the counterfeit coin and it is heavy."<<endl;;
37 break;
38 }
39 else if(c[i][0]==-res[0] && c[i][1]==-res[1] && c[i][2]==-res[2]) {
40 cout<<char('A'+i)<<" is the counterfeit coin and it is light."<<endl;;
41 break;
42 }
43 }
44 }
45 return 0;
46}