码迷,mamicode.com
首页 > 其他好文 > 详细

HDU1172 暴力水

时间:2014-09-10 00:25:09      阅读:258      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   os   io   java   strong   for   

猜数字

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2722    Accepted Submission(s): 1585


Problem Description
猜数字游戏是gameboy最喜欢的游戏之一。游戏的规则是这样的:计算机随机产生一个四位数,然后玩家猜这个四位数是什么。每猜一个数,计算机都会告诉玩家猜对几个数字,其中有几个数字在正确的位置上。
比如计算机随机产生的数字为1122。如果玩家猜1234,因为1,2这两个数字同时存在于这两个数中,而且1在这两个数中的位置是相同的,所以计算机会告诉玩家猜对了2个数字,其中一个在正确的位置。如果玩家猜1111,那么计算机会告诉他猜对2个数字,有2个在正确的位置。
现在给你一段gameboy与计算机的对话过程,你的任务是根据这段对话确定这个四位数是什么。
 

 

Input
输入数据有多组。每组的第一行为一个正整数N(1<=N<=100),表示在这段对话中共有N次问答。在接下来的N行中,每行三个整数A,B,C。gameboy猜这个四位数为A,然后计算机回答猜对了B个数字,其中C个在正确的位置上。当N=0时,输入数据结束。
 

 

Output
每组输入数据对应一行输出。如果根据这段对话能确定这个四位数,则输出这个四位数,若不能,则输出"Not sure"。
 

 

Sample Input
6
4815 2 1
5716 1 0
7842 1 0
4901 0 0
8585 3 3
8555 3 2
2
4815 0 0
2999 3 3 0
 

 

Sample Output
3585
Not sure
 
 
枚举1000-9999一一比对,符合条件个数为num,num>1输出Not sure ;num==1输出数字;num==0什么也不输出。
思路有了代码就很好写了。
 
代码:
 1 #include <cstdio>
 2 #include <algorithm>
 3 #include <cstring>
 4 #include <iostream>
 5 #include <queue>
 6 #include <vector>
 7 
 8 using namespace std;
 9 
10 queue<int>Q;
11 
12 int pd(int num1,int x,int y,int num){
13     int n1, n2;
14     n1=n2=0;
15     int a[4], b[4], c1[10], c2[10];
16     memset(c1,0,sizeof(c1));
17     memset(c2,0,sizeof(c2));
18     int i, f1, f2;
19     f1=f2=1;
20     for(i=0;i<4;i++){
21         a[i]=num1%10;
22         c1[a[i]]++;
23         num1/=10;
24     }
25     for(i=0;i<4;i++){
26         b[i]=num%10;
27         c2[b[i]]++;
28         num/=10;
29     }
30     for(i=0;i<4;i++){
31         if(a[i]==b[i]) n1++;
32     }
33     for(i=0;i<10;i++){
34         n2+=min(c1[i],c2[i]);
35     }
36     if(n2==x&&n1==y) return 1;
37     else return 0;
38 }
39 
40 main()
41 {
42     int n, i, j, k;
43     int x, y, z, num, size;
44     while(scanf("%d",&n)==1&&n){
45         scanf("%d %d %d",&x,&y,&z);
46         while(!Q.empty()) Q.pop();
47         for(i=1000;i<=9999;i++){
48             if(pd(x,y,z,i)) Q.push(i);
49         }
50         n--;
51         while(n--){
52             scanf("%d %d %d",&x,&y,&z);
53             size=Q.size();
54             if(size==0) continue;
55             while(size--){
56                 num=Q.front();
57             //    printf("%d ",num);
58                 Q.pop();
59                 if(pd(x,y,z,num)) Q.push(num);
60             }
61         //    cout<<endl<<endl;
62         }
63         if(Q.size()==1) printf("%d\n",Q.front());
64         else if(Q.size()==0) continue;
65         else printf("Not sure\n");
66     }
67 }

 

HDU1172 暴力水

标签:des   style   blog   color   os   io   java   strong   for   

原文地址:http://www.cnblogs.com/qq1012662902/p/3963601.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!