zoukankan      html  css  js  c++  java
  • HDOJ1172解题报告【暴力】

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1172

    题目概述:

      对于每组数据,先给出一个n,然后n行每行一个四位数,然后两个数b,c表示与答案有b个数相同,c个数在同一位置上,对于所有的n个给出的四位数,如果推出的答案唯一则输出,否则输出“Not sure”(不含引号)

    大致思路:

      首先我想的是用搜索。穷极一生之力写了100+行的代码发现细节越来越多是怎么回事……

      遂决定换用别的思路,细细一想发现这难道不是暴力枚举答案?!!!因为答案是四位数所以只要从1000~9999枚举,对于每一个数判断是否满足所给的n个条件即可。

    复杂度分析:

      由思路可以很明显的算出复杂度为O(10000*n*T),其中n的含义如题述,T为数据组数。

    代码:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstdlib>
     4 #include <cmath>
     5 #include <vector>
     6 #include <ctime>
     7 #include <map>
     8 #include <queue>
     9 #include <cstring>
    10 #include <algorithm>
    11 using namespace std;
    12 
    13 #define sacnf scanf
    14 #define maxn 1010
    15 #define inf 1061109567
    16 #define Eps 0.001
    17 #define PI 3.1415927
    18 #define mod 1000000007
    19 void Swap(int &a,int &b) {int t=a;a=b;b=t;}
    20 int Abs(int x) {return (x<0)?-x:x;}
    21 typedef long long ll;
    22 
    23 struct node
    24 {
    25     char num[5];
    26     int b,c;
    27 } a[maxn];
    28 
    29 bool match(int x,node y)
    30 {
    31     int vis[10];int tb=0,tc=0;
    32     memset(vis,0,sizeof(vis));
    33     for(int i=3;i>=0;i--)
    34     {
    35         int t=x%10;
    36         vis[t]++;x/=10;
    37         if(t==y.num[i]-'0') tc++;
    38     }
    39     for(int i=0;i<4;i++)
    40     {
    41         if(vis[y.num[i]-'0'])
    42         {
    43             tb++;
    44             vis[y.num[i]-'0']--;
    45         }
    46     }
    47     if(tb==y.b&&tc==y.c) return true;
    48     return false;
    49 }
    50 
    51 int main()
    52 {
    53     //freopen("data.in","r",stdin);
    54     //freopen("data.out","w",stdout);
    55     //clock_t st=clock();
    56     int n;
    57     while(~scanf("%d",&n))
    58     {
    59         if(n==0) break;
    60         for(int i=1;i<=n;i++)
    61         {
    62             scanf("%s",a[i].num);
    63             scanf("%d%d",&a[i].b,&a[i].c);
    64         }
    65         bool is_ans=false;;int ans=0;
    66         for(int tmp=1000;tmp<=9999;tmp++)
    67         {
    68             bool flag=true;
    69             for(int i=1;i<=n;i++)
    70             {
    71                 if(!match(tmp,a[i]))
    72                 {
    73                     flag=false;break;
    74                 }
    75             }
    76             if(flag)
    77             {
    78                 if(ans!=0)
    79                 {
    80                     is_ans=false;
    81                     break;
    82                 }
    83                 else
    84                 {
    85                     ans=tmp;
    86                     is_ans=true;
    87                 }
    88             }
    89         }
    90         if(!is_ans) printf("Not sure
    ");
    91         else printf("%d
    ",ans);
    92     }
    93     //clock_t ed=clock();
    94     //printf("
    
    Time Used : %.5lf Ms.
    ",(double)(ed-st)/CLOCKS_PER_SEC);
    95     return 0;
    96 }
  • 相关阅读:
    【NOIP2007】守望者的逃离
    20200321(ABC)题解 by 马鸿儒 孙晨曦
    20200320(ABC)题解 by 王一帆
    20200319(ABC)题解 by 王一帆 梁延杰 丁智辰
    20200314(ABC)题解 by 董国梁 蒋丽君 章思航
    20200309(ABC)题解 by 梁延杰
    20200307(DEF)题解 by 孙晨曦
    20200306(ABC)题解 by 孙晨曦
    20200305(DEF)题解 by 孙晨曦
    20200303(ABC)题解 by 王锐,董国梁
  • 原文地址:https://www.cnblogs.com/CtrlKismet/p/6238476.html
Copyright © 2011-2022 走看看