TZU2014年省赛个人热身赛1 1197:Number Guessing

描述

Number Guessing is a computer game. First, the computer chooses four different digits, you need to guess these four digits in the fewest times,for each guess, the computer will show a judgement in the form of "#A#B", "#" is a number 0~4. "#A" shows how many digits you guessed with both correct value and position. "#B" shows how many digits you guessed with correct value. For example, the computer chose 1234, and you guessed 6139, the computer will show "1A2B" for you have number "1" correct value but wrong position and number "3" correct value with correct position.Thus the computer gives you the judgement of "1A2B". Now you have memorized the digits you guessed and the judgements you got, you feel like you can figure out the correct answer. Life is filled with wisdom, isn't it?

输入

There are several test cases. For each test case, the first line contains a single positive integer N indicates the times you can guess,the following N lines is the record of the guess, in the form:

#### #A#B

The first four numbers is the numbers guessed,then the judgements for your guess.The input is terminated by a negative integer.

输出

For each test case, output a single line contains exactly four digits that the computer has chosen. You may assume that each test case gives you enough information, so you can figure out the correct answer.

样例输入

21234 2A4B1243 0A4B30732 3A3B1526 0A0B4567 0A2B-1

样例输出

21340734
 
猜数字,A代表几个数字正确且在正确位置,B为几个大小正确
直接暴力即可
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

char s1[10000][10],s2[10000][10];
int s[10];
int hash[10];

int main()
{
    int t,i,j,n,ans;
    while(~scanf("%d",&n),n>0)
    {
        for(i = 0;i<n;i++)
        scanf("%s%s",s1[i],s2[i]);
        for(i = 0;i<10000;i++)
        {
            t = i;
            s[3] = t%10;
            s[2] = (t/10)%10;
            s[1] = (t/100)%10;
            s[0] = (t/1000)%10;
            if(t/1000>0 && s[0]==s[1]||s[0]==s[2]||s[0]==s[3]||s[1]==s[2]||s[1]==s[3]||s[2]==s[3])
            continue;
            else if(t/100>0 && s[1]==s[2]||s[1]==s[3]||s[2]==s[3])
            continue;
            else if(t/10>0 && s[2]==s[3])
            continue;
            int cnt = 0;
            for(j = 0;j<n;j++)
            {
                int a = 0,b = 0;
                memset(hash,0,sizeof(hash));
                hash[s1[j][0]-'0'] = hash[s1[j][1]-'0'] = hash[s1[j][2]-'0'] = hash[s1[j][3]-'0'] = 1;
                for(int k = 0;k<4;k++)
                {
                    if(s[k] == s1[j][k]-'0')
                    a++;
                    if(hash[s[k]])
                    b++;
                }
                if(a == s2[j][0]-'0' && b == s2[j][2]-'0')
                cnt++;
            }
            if(cnt == n)
            {
                ans = i;
                break;
            }
        }
        printf("%04d\n",ans);
    }

    return 0;
}

你可能感兴趣的:(水,TZU)