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
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
思路:1000~9999暴力枚举过去,只要看看有无唯一满足条件的就好了
本题唯一有意思的就是5888 & 8555怎么判断有几个数字相等,用一个vis[]数字,记录该数字如果相等了,color掉,下次即使有相等也不能再用,一个数字只能color一个,所以注意break,刚开始没有注意到这一点,只是一味抄别人代码
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <algorithm>

const int inf = 0x3f3f3f;
const int MAXN = 1e2+10;

using namespace std;

bool check1(int num1,int num2,int t){
    int a[4],b[4],c[4],m = 0;
    for(int i=0;i<4;i++){
        a[i] = num1%10;
        num1 /= 10;
        b[i] = num2%10;
        num2 /= 10;
        c[i] = 0;
    }
    for(int i=0;i<4;i++){
        for(int j=0;j<4;j++){
            if(!c[j]&&a[i]==b[j]){
                m++;
                c[j] = 1;
                break;
            }
        }
    }
    if(m==t)return true;
    return false;
}

bool check2(int num1,int num2,int t){
    int a[4],b[4],m=0;
    for(int i=0;i<4;i++){
        a[i] = num1%10;
        num1 /= 10;
        b[i] = num2%10;
        num2 /= 10;
        if(a[i]==b[i]){
            m++;
        }
    }
    if(m==t)return true;
    return false;
}

int main()
{
    int n,j,cnt,res;
    int a[MAXN],b[MAXN],c[MAXN];
   // if(check1(5555,8885,4))cout<<"debug"<<endl;

    while(scanf("%d",&n)!=EOF&&n){
        cnt = 0;
        for(int i=0;i<n;i++){
            scanf("%d%d%d",&a[i],&b[i],&c[i]);
        }

        for(int i=1000;i<=9999;i++){
            for(j=0;j<n;j++){
                if(!check1(i,a[j],b[j]))break;
                if(!check2(i,a[j],c[j]))break;
            }
            if(j==n){
                cnt++;
                res = i;
            }
        }
        if(cnt==0||cnt>=2)cout<<"Not sure"<<endl;
        else{
            cout<<res<<endl;
        }

    }
    //cout << "Hello world!" << endl;
    return 0;
}
View Code
在一个谎言的国度,沉默就是英雄
原文地址:https://www.cnblogs.com/EdsonLin/p/5490100.html