• HDU1172猜数字 [模拟]


    1.题意

    任务是猜一个四位数,每次尝试后会给出这次猜中了几个数字和猜中了几个位置,求能否根据尝试的记录给出答案

    2.分析

    数据给出查询次数和每次查询的数及其有几个数和几个位置符合,值得注意的是,猜对的数的个数计算不能重复,比如样例里要猜1122,尝试1234,这里只有两个数猜中了,1122中的前两个1只能算一次

    注意题目限定是四位数所以直接枚举1000-9999就可以了

    3.代码

     1 # include <iostream>
     2 # include <cstdio>
     3 using namespace std;
     4 const int maxn=105;
     5 int N,ans;
     6 struct Node
     7 {
     8     int test_num,a,b;
     9     Node(){}
    10     Node(int tt,int aa,int bb)
    11     {
    12         test_num=tt;
    13         a=aa;
    14         b=bb;
    15     }
    16 }Test[maxn];
    17 void Init()
    18 {
    19     for(int i=0;i<N;i++)
    20         scanf("%d%d%d",&Test[i].test_num,&Test[i].a,&Test[i].b);
    21     ans=-1;
    22 }
    23 bool judge(int num)
    24 {
    25     int np[4],nptest[4];
    26     np[0]=num%10;
    27     np[1]=(num%100)/10;
    28     np[2]=(num%1000)/100;
    29     np[3]=num/1000;
    30     for(int i=0;i<N;i++)
    31     {
    32         nptest[0]=Test[i].test_num%10;
    33         nptest[1]=(Test[i].test_num%100)/10;
    34         nptest[2]=(Test[i].test_num%1000)/100;
    35         nptest[3]=Test[i].test_num/1000;
    36         int tempa,tempb;
    37         tempa=tempb=0;
    38         int vis[4];
    39         for(int j=0;j<4;j++) vis[j]=0; 
    40         for(int j=0;j<4;j++)
    41         {
    42             if(np[j]==nptest[j]) tempb++;
    43             for(int k=0;k<4;k++)
    44             {
    45                 if(np[j]==nptest[k]&&vis[k]==0)
    46                 {
    47                     vis[k]=1;
    48                     tempa++;
    49                     break; 
    50                 }
    51             }
    52         }
    53         if(tempa==Test[i].a&&tempb==Test[i].b)
    54             continue;
    55         else return false;
    56     }
    57     return true;
    58 }
    59 int getcnt()
    60 {
    61     int cnt=0;
    62     for(int i=1000;i<10000;i++)
    63         if(judge(i)) 
    64         {
    65             ans=i;
    66             cnt++;
    67         }        
    68     return cnt;
    69 }
    70 void Solve()
    71 {
    72     int temp=getcnt();
    73     if(temp==1) printf("%d
    ",ans);
    74     else printf("Not sure
    ");
    75 }
    76 int main()
    77 {
    78     while(scanf("%d",&N)!=EOF)
    79     {
    80         if(N==0) break;
    81         Init();
    82         Solve();
    83     }
    84     return 0;
    85 }
  • 相关阅读:
    暑假第一周进度总结
    学习记录(Python字典)
    学习记录(Python元组)
    学习记录(完成实验一二安装Scala)
    学习记录(安装Sbt)
    学习记录(安装spark)
    学习记录(Python列表)
    学习记录(Python循环结构)
    学习记录(Python选择结构)
    学习记录(Python算数运算符与if语句)
  • 原文地址:https://www.cnblogs.com/cnXuYang/p/8653239.html
Copyright © 2020-2023  润新知