A - 猜数字

http://acm.hdu.edu.cn/showproblem.php?pid=1172

猜数字

猜数字游戏是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
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 using namespace std;
 6 
 7 const int N = 110;
 8 struct Arr{
 9     int a,b,c;
10 }arr[N];
11 int hashA[N],hashB[N];
12 bool judge(int y,int n)
13 {
14     memset(hashA,0,sizeof(hashA));  //初始化数组hashA
15     int A1,B1,C1,D1,A2,B2,C2,D2;
16     A1 = y % 10; hashA[A1]++;  //个位
17     B1 = y / 10 % 10; hashA[B1]++;  //十位
18     C1 = y / 100 % 10; hashA[C1]++;  //百位
19     D1 = y / 1000 % 10; hashA[D1]++;  //千位
20     for(int i = 0;i < n ; i++)  //判断键盘输入的 n 个数
21     {
22         memset(hashB,0,sizeof(hashB));  //初始化数组hashB
23         int x = arr[i].a;
24         A2 = x % 10; hashB[A2]++;
25         B2 = x / 10 % 10; hashB[B2]++;
26         C2 = x / 100 % 10; hashB[C2]++;
27         D2 = x / 1000 % 10; hashB[D2]++;
28         int cnt1 = 0, cnt2 = 0;  //cnt1记录在正确位置上的正确的个数
29         if(A1 == A2) cnt1++;
30         if(B1 == B2) cnt1++;
31         if(C1 == C2) cnt1++;
32         if(D1 == D2) cnt1++;
33         if(cnt1 != arr[i].c) return false;
34         for(int q = 0; q < 10; q++) cnt2 += min(hashA[q],hashB[q]); //单个数字个数介于 0 ~ 10
35         if(cnt2 != arr[i].b) return false;
36     }
37     return true;
38 }
39 
40 int main()
41 {
42     int n;
43     while(~scanf("%d",&n) && n)
44     {
45         for(int i = 0; i < n;i++)  //输入 n 组数据
46             scanf("%d %d %d",&arr[i].a,&arr[i].b,&arr[i].c);  //输入的数,猜对 B个数,C个在正确位置上
47         int ans = -1;
48         for(int i = 1000; i < 9999; i++)
49         {
50             if(judge(i,n))
51             {
52                 if(ans != -1) { ans = 0;break; }  //第二次匹配,结束匹配
53                 ans = i;   //第一次匹配成功,继续匹配下一个数
54             }
55         }
56         if(!ans) printf("Not sure\n"); 
57         else printf("%d\n",ans);
58     }
59     return 0;
60 }

猜你喜欢

转载自www.cnblogs.com/Edviv/p/11441226.html
今日推荐