zoukankan      html  css  js  c++  java
  • 猜数字-暴力枚举

    A - 猜数字
    Time Limit:10000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

    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
     
    /*
    Author: 2486
    Memory: 1604 KB		Time: 46 MS
    Language: C++		Result: Accepted
    */
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    const int maxn=100+5;
    int n;
    struct state {
        int num,c,d;
    } sts[maxn];
    int B(int x,int y) {//推断包括几个符合条件的数
        int a[4],b[4];
        int al=0,bl=0;
        while(x) {
            a[al++]=x%10;
            x/=10;
        }
        while(y) {
            b[bl++]=y%10;
            y/=10;
        }
        int ans=0;
        int vis[15]= {0};
        for(int i=0; i<4; i++) {
            vis[b[i]]++;
        }
        for(int i=0; i<4; i++) {
            if(vis[a[i]])
                vis[a[i]]--;
        }
        for(int i=0; i<10; i++) {
            ans+=vis[i];
        }
        return 4-ans;
    }
    int D(int x,int y) {//推断含有一个位置正确的数
        int a[4],b[4];
        int al=0,bl=0;
        while(x) {
            a[al++]=x%10;
            x/=10;
        }
        while(y) {
            b[bl++]=y%10;
            y/=10;
        }
        int ans=0;
        for(int i=0; i<4; i++) {
            if(b[i]==a[i])ans++;
        }
        return ans;
    }
    bool C(int m) {
        for(int i=0; i<n; i++) {
            if(!(B(sts[i].num,m)==sts[i].c&&D(sts[i].num,m)==sts[i].d)) {
                return false;
            }
        }
        return true;
    }
    int main() {
        while(~scanf("%d",&n),n) {
            for(int i=0; i<n; i++) {
                scanf("%d%d%d",&sts[i].num,&sts[i].c,&sts[i].d);
            }
            int ds=0;
            int flag=0;
            for(int i=1000; i<10000; i++) {
                if(C(i)) {
                    ds=i;
                    flag++;
                }
            }
            if(flag!=1)printf("Not sure
    ");
            else printf("%d
    ",ds);
        }
        return 0;
    }


  • 相关阅读:
    Centos 5.5 Lamp源码包安装编译 新风宇宙
    Linux系统日志管理 新风宇宙
    ubuntu设置时区,网上同步时间 新风宇宙
    ubuntu vim输入中文设置(SecureCRT下) 新风宇宙
    DIV+CSS容易犯的十个错误 新风宇宙
    apache性能优化 新风宇宙
    java里面main函数为什么要用static修饰
    如何设计mysql数据库和数据表
    PHP 图片验证码
    PHP免费空间选择方法概述
  • 原文地址:https://www.cnblogs.com/lcchuguo/p/5369863.html
Copyright © 2011-2022 走看看