hdu1172猜数字

来源:互联网 发布:多维元素片 知乎 编辑:程序博客网 时间:2024/06/05 20:16

题目链接:

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

题目

猜数字

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


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
64815 2 15716 1 07842 1 04901 0 08585 3 38555 3 224815 0 02999 3 30
 

Sample Output
3585Not sure
 

Author
lwg
 

Recommend
We have carefully selected several similar problems for you:  1106 1175 1174 1180 1108
 
这个题目的思路是:

从1000到9999进行暴力枚举。。。

需要满足两个条件:

1:题目所给的条件和目前枚举的值要有相同的位数的值要相等。。

2:将目前枚举的数和题目所给的数进行枚举,看所给的条件的数与目前枚举的的数的出现相同的数相等的数有多少个。。但是重复的书不算。。所以开个标志变量。。。

所以我的暴力枚举解法如下:

#include<cstdio>#include<cstring>const int maxn=100+10;struct node{    int a,b,c;}point[maxn];int a[5],b[5];int mark[5];void pre_deal(int c,int d){    a[1]=c/1000;    a[2]=c/100%10;    a[3]=c/10%10;    a[4]=c%10;    b[1]=d/1000;    b[2]=d/100%10;    b[3]=d/10%10;    b[4]=d%10;}int judge(int x,int y){     int count1,count2;     count1=count2=0;     memset(a,0,sizeof(a));     memset(b,0,sizeof(b));     memset(mark,0,sizeof(mark));     pre_deal(x,point[y].a);     for(int i=1;i<=4;i++)        {             if(a[i]==b[i])               count1++;        }    // printf("count1:%d\n",count1);     if(count1!=point[y].c)         return 0;     for(int i=1;i<=4;i++)        for(int j=1;j<=4;j++)       {          if(a[i]==b[j]&&!mark[j])          {             mark[j]=1;             count2++;             break;          }       }    //printf("count2:%d\n",count2);    if(count2!=point[y].b)        return 0;    else        return 1;}int main(){    int n,ok;    int count,ans;    while(scanf("%d",&n)!=EOF&&n)    {        count=0;        for(int i=1;i<=n;i++)            scanf("%d%d%d",&point[i].a,&point[i].b,&point[i].c);        for(int i=1000;i<=9999;i++)            {                ok=1;                for(int j=1;j<=n;j++)                 {                     ok=judge(i,j);                     if(!ok)                        break;                 }                if(ok)                {                    count++;                    ans=i;                }                if(count==2)                  break;            }     //   printf("count:%d\n",count);        if(count==1)            printf("%d\n",ans);        else            printf("Not sure\n");     }    return 0;}

0 0