码迷,mamicode.com
首页 > 其他好文 > 详细

HDU猜数字

时间:2017-01-13 01:02:38      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:cout   pac   正整数   产生   lag   是什么   cpp   ret   输入   

G - 猜数字

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

 

 

——————-

 

题目比较水,主要是卡了一下 如何判断 两个数字有几个数字相同这个问题。

后来看样例才明白什么意思。。

 

代码如下:

#include<iostream>

#include<cstdio>

#include<algorithm>

#include<cmath>

using namespace std;

#define MAXN 110

 

int a[MAXN][4] ;

 

inthow_correct( int a , int b ) {

       int c[5],d[5];

       c[1] = a / 1000 ;

       c[2] = a / 100 % 10 ;

       c[3] = a / 10 % 10 ;

       c[4] = a % 10 ;

       d[1] = b / 1000 ;

       d[2] = b / 100 % 10;

       d[3] = b / 10 % 10 ;

       d[4] = b % 10 ;

       int num = 0;

       for( int i = 1 ; i <= 4 ; i++)

             if( c[i] == d[i] )

                    num++;

       return num ;

}

//4815(2 1 ) 3585

inthow_many( int a , int b ) {

       int c[5],d[5];

       c[1] = a / 1000 ;

       c[2] = a / 100 % 10 ;

       c[3] = a / 10 % 10 ;

       c[4] = a % 10 ;

       d[1] = b / 1000 ;

       d[2] = b / 100 % 10;

       d[3] = b / 10 % 10 ;

       d[4] = b % 10 ;

       int num = 0;

       bool flag_c[]= {false,false,false,false,false};

       bool flag_d[]= {false,false,false,false,false};

       for( int i = 1 ; i <= 4 ; i++) {

             if( c[i] == d[i] ) {

                    num++;

                    flag_c[i] =true;

                    flag_d[i]= true;

             }

       }

       for( int i = 1 ; i <= 4 ; i++) {

             if( flag_c[i] == false ) {

                    for( int j = 1 ; j<=4 ; j++) {

                          if( c[i] == d[j] && flag_d[j] == false ) {

                                 num++;

                                 flag_c[i] = true;

                                 flag_d[j] = true ;

                                 break;

                          }

                    }

             }

       }

       return num ;

}

 

/*6

4815 2 1

5716 1 0

7842 1 0

4901 0 0

8585 3 3

8555 3 2*/

intmain() {

 

 

       int n ;

       while( cin >> n ) {

             if( n == 0 ) break ;

             for( int i = 1 ; i <= n ; i++)

                    cin>> a[i][1] >> a[i][2] >> a[i][3] ;

       //    for(int l = 1 ; l<=n ; l++) 

             //     cout<< how_many( 3585,a[l][1]) <<‘\t‘ << how_correct( 3585 ,a[l][1] ) << endl ;

       //    return0 ; 

             int cnt = 0 ;

             int k ;

             int ans ;

             for(  int j = 3585 ; j<= 9999 ; j++ ) {

                    int flag = 1 ;

                    for( k = 1 ; k<=n ; k++) {

                          if(how_correct( j , a[k][1] ) == a[k][3] &&how_many(j , a[k][1] ) == a[k][2] )

                          //     {cout<<j <<‘\t‘ << k<<‘\t‘ <<"correct"<<endl;continue ;}

                                 continue ;

                          else {

                                 flag = 0 ;

                                 break;

                          }

                    }

                    if( flag == 1 ) {

                          cnt++ ;

                          ans= j ;

                    }

             }

             if( cnt == 1 )

                    cout<< ans << endl ;

             else

                    cout<< "Not sure" << endl ;

       }

       return 0 ;

}

 

HDU猜数字

标签:cout   pac   正整数   产生   lag   是什么   cpp   ret   输入   

原文地址:http://www.cnblogs.com/xiekeyi98/p/6280288.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!