HDU1172 猜数字

               
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
 


 

知道题意后直接暴力枚举即可

#include<stdio.h>#include<cstring>using namespace std;const int MAXN=100;struct Node{    int a,b,c;} node[MAXN];bool Judge(int k,int number){    int num1[5],num2[5];    bool mark[5];    for(int i=1; i<=4; i++)mark[i]=false;    num1[1]=node[k].a/1000;    num1[2]=(node[k].a%1000)/100;    num1[3]=(node[k].a%100)/10;    num1[4]=(node[k].a%10);    num2[1]=number/1000;    num2[2]=(number%1000)/100;    num2[3]=(number%100)/10;    num2[4]=(number%10);    int count=0;    for(int i=1; i<=4; i++)//相同位置相同的数    {        if(num1[i]==num2[i])count++;    }    if(count!=node[k].c)return false;//如果个数不符就返回错    count=0;    for(int i=1; i<=4; i++)//相同数字的个数    {        for(int j=1; j<=4; j++)        {            if(num1[i]==num2[j]&&!mark[j])            {                mark[j]=true;                count++;                break;            }        }    }    if(count!=node[k].b)return false;    return true;}int main(){    int n;    while(~scanf("%d",&n)&&n)    {        for(int i=1; i<=n; i++)        {            scanf("%d%d%d",&node[i].a,&node[i].b,&node[i].c);        }        int count=0,result;        bool flag=true;        for(int num=1000; num<=9999; num++)//暴力枚举        {            for(int i=1; i<=n; i++)            {                flag=Judge(i,num);                if(!flag)break;            }            if(flag)            {                count++;                result=num;            }        }        if(count==1)        {            printf("%d\n",result);        }        else            printf("Not sure\n");    }    return 0;}


 

           

猜你喜欢

转载自blog.csdn.net/qq_44919369/article/details/89458529