北大poj 1013 Counterfeit Dollar 一种解法

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后面没有加句号"."!!!!

 

 1 #include  < iostream >
 2 #include  < string >
 3 #include  < fstream >
 4 #include  < memory.h >
 5 using   namespace  std;
 6
 7 int  main()
 8 ExpandedBlockStart.gifContractedBlock.gif {
 9    int c[12][3];
10    int res[3];
11    int n, i, j;
12
13    cin>>n;
14ExpandedSubBlockStart.gifContractedSubBlock.gif    while(n--{
15        memset(c, 0sizeof(c));
16        memset(res, 0sizeof(res));
17        string s1, s2, s3;
18ExpandedSubBlockStart.gifContractedSubBlock.gif        for(i=0; i<3; i++){
19            cin>>s1>>s2>>s3;
20ExpandedSubBlockStart.gifContractedSubBlock.gif            for(j=0; j<s1.size(); j++{
21                c[s1[j]-'A'][i] = 1;
22                c[s2[j]-'A'][i] = -1;
23            }

24ExpandedSubBlockStart.gifContractedSubBlock.gif            if(s3=="even"){
25                res[i] = 0;
26            }

27ExpandedSubBlockStart.gifContractedSubBlock.gif            else if(s3=="up"){
28                res[i] = 1;
29            }

30ExpandedSubBlockStart.gifContractedSubBlock.gif            else {
31                res[i] = -1;
32            }

33        }

34ExpandedSubBlockStart.gifContractedSubBlock.gif        for(i=0; i<13; i++{
35ExpandedSubBlockStart.gifContractedSubBlock.gif            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            }

39ExpandedSubBlockStart.gifContractedSubBlock.gif            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}

 

下面是枚举的方法:

 1 int  main()
 2 ExpandedBlockStart.gifContractedBlock.gif {
 3    int c[12];
 4    int res[3];
 5    int n, i, j;
 6    cin>>n;
 7ExpandedSubBlockStart.gifContractedSubBlock.gif    while(n--{
 8        memset(c, 0sizeof(c));
 9        memset(res, 0sizeof(res));
10        string s1[3], s2[3], s3[3];
11ExpandedSubBlockStart.gifContractedSubBlock.gif        for(i=0; i<3; i++){
12            cin>>s1[i]>>s2[i]>>s3[i];
13        }

14ExpandedSubBlockStart.gifContractedSubBlock.gif        for(i=0; i<12; i++{
15            memset(c, 0sizeof(c));
16            c[i] = 1;
17            int left, right;
18ExpandedSubBlockStart.gifContractedSubBlock.gif            for(j=0; j<3; j++{
19                left = 0; right = 0;
20ExpandedSubBlockStart.gifContractedSubBlock.gif                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"
25ExpandedSubBlockStart.gifContractedSubBlock.gif                    || (left==right && s3[j]!="even") ){
26                    break;
27                }

28            }

29ExpandedSubBlockStart.gifContractedSubBlock.gif            if(j==3{
30                cout<<char('A'+i)<<" is the counterfeit coin and it is heavy."<<endl;
31            }

32            c[i] = -1;
33ExpandedSubBlockStart.gifContractedSubBlock.gif            for(j=0; j<3; j++{
34                left = 0; right = 0;
35ExpandedSubBlockStart.gifContractedSubBlock.gif                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"
40ExpandedSubBlockStart.gifContractedSubBlock.gif                    || (left==right && s3[j]!="even") ){
41                    break;
42                }

43            }

44ExpandedSubBlockStart.gifContractedSubBlock.gif            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()
 8ExpandedBlockStart.gifContractedBlock.gif{
 9    int c[12][3];
10    int res[3];
11    int n, i, j;
12
13    cin>>n;
14ExpandedSubBlockStart.gifContractedSubBlock.gif    while(n--{
15        memset(c, 0sizeof(c));
16        memset(res, 0sizeof(res));
17        string s1, s2, s3;
18ExpandedSubBlockStart.gifContractedSubBlock.gif        for(i=0; i<3; i++){
19            cin>>s1>>s2>>s3;
20ExpandedSubBlockStart.gifContractedSubBlock.gif            for(j=0; j<s1.size(); j++{
21                c[s1[j]-'A'][i] = 1;
22                c[s2[j]-'A'][i] = -1;
23            }
24ExpandedSubBlockStart.gifContractedSubBlock.gif            if(s3=="even"){
25                res[i] = 0;
26            }
27ExpandedSubBlockStart.gifContractedSubBlock.gif            else if(s3=="up"){
28                res[i] = 1;
29            }
30ExpandedSubBlockStart.gifContractedSubBlock.gif            else {
31                res[i] = -1;
32            }
33        }
34ExpandedSubBlockStart.gifContractedSubBlock.gif        for(i=0; i<13; i++{
35ExpandedSubBlockStart.gifContractedSubBlock.gif            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            }
39ExpandedSubBlockStart.gifContractedSubBlock.gif            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}

转载于:https://www.cnblogs.com/orangeman/archive/2009/07/10/1520663.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值