hdu1172

xiaoxiao2021-02-27  528

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

刚开始看这道题目的时候没有什么头绪,后来看了两个题解发现还是挺简单的,因为是4位数字数据量很小,我们完全可以从1000枚举到9999.

首先检查C位置,如果一个数的某几个位置数正确,那么加入咱们猜的这个数字是正确的那么它的某几个位肯定和猜的那个数一样。

再检查B位置,一共有几个数字是正确的。

代码如下:

#include<iostream> #include<string> #include<cstring> #include<cmath> #include<algorithm> #include<stdio.h> using namespace std; struct Nod{     int a,b,c; }array[111]; bool judge(int k,int num) {     int num1[5],num2[5];     int mark[5];     memset(mark,0,sizeof(mark));     num1[0] = (array[k].a) / 1000;     num1[1] = (array[k].a % 1000) / 100;     num1[2] = (array[k].a % 100) / 10;     num1[3] = (array[k].a % 10);          num2[0] = (num) / 1000;     num2[1] = (num % 1000) / 100;     num2[2] = (num % 100) / 10;     num2[3] = (num % 10);     int i,j;     int count = 0;     for(i = 0;i < 4;i++)     {         if(num1[i] == num2[i])         count++;     }     if(count != array[k].c)     return false;     count = 0;     for(i = 0;i < 4;i++)     {         for(j = 0;j < 4;j++)         {             if(num1[i] == num2[j]&&mark[j] != 1)             {                 mark[j] = true;                 count++;                 break;                 }         }     }     if(count != array[k].b)     return false;     return true; } int main() {     int n,i,j;     while(~scanf("%d",&n))     {         if(n == 0)         break;         for(i = 0;i < n;i++)         {             cin>>array[i].a>>array[i].b>>array[i].c;         }         bool flag = true;         int count = 0,result;         for(i = 1000;i<=9999;i++)         {             for(j = 0;j < n;j++)             {                 flag = judge(j,i);                 if(!flag)                 break;             }             if(flag)             {                 count++;                 result = i;             }         }         if(count == 1)         cout<<result<<endl;         else         cout<<"Not sure"<<endl;     }               return 0; }

转载请注明原文地址: https://www.6miu.com/read-151.html

最新回复(0)