zoukankan      html  css  js  c++  java
  • poj 1465 Multiple(bfs+余数判重)

    题意:给出m个数字,要求组合成能够被n整除的最小十进制数。

    分析:用到了余数判重,在这里我详细的解释了。其它就没有什么了。

     1 #include<cstdio>
     2 #include<cmath>
     3 #include<cstring>
     4 #include<queue>
     5 #include<algorithm>
     6 using namespace std;
     7 
     8 const int MAXN=5555;
     9 const int N=22;
    10 
    11 struct Node{
    12     int pre;
    13     int r;
    14     int d;
    15 };
    16 
    17 int vis[MAXN];
    18 int num[N];
    19 int n,c,m;
    20 Node q[MAXN];
    21 
    22 void print(int x)
    23 {
    24     if(q[x].pre==-1)
    25         return ;
    26     print(q[x].pre);
    27     printf("%d",q[x].d);
    28 }
    29 
    30 int bfs()
    31 {
    32     memset(vis,0,sizeof(vis));
    33     int dl,dr;
    34     dl=dr=0;
    35     Node u,v;
    36     u.pre=-1;
    37     u.d=0;
    38     u.r=0;
    39     q[dr++]=u;
    40     vis[0]=1;
    41 
    42     int ok=0;
    43     while(dl<dr)
    44     {
    45         u=q[dl++];
    46         for(int i=0;i<m;i++)
    47         {
    48             int r=u.r*10+num[i];
    49             if(r>=n&&r%n==0){
    50                 print(dl-1);
    51                 printf("%d
    ",num[i]);
    52                 return 1;
    53             }
    54             r=r%n;
    55             if(!vis[r]){
    56                 vis[r]=1;
    57                 v.r=r;
    58                 v.d=num[i];
    59                 v.pre=dl-1;
    60                 q[dr++]=v;
    61             }
    62         }
    63     }
    64     return -1;
    65 }
    66 
    67 int main()
    68 {
    69     int T;
    70     while(~scanf("%d%d",&n,&m))
    71     {
    72         memset(num,-1,sizeof(num));
    73         for(int i=0;i<m;i++)
    74             scanf("%d",&num[i]);
    75         sort(num,num+m);
    76 
    77         if(n==0)
    78             printf("0
    ");
    79         else{
    80             int ans=bfs();
    81             if(ans==-1)
    82                 printf("0
    ");
    83         }
    84     }
    85     return 0;
    86 }
    View Code
  • 相关阅读:
    bootstrap class sr-only 什么意思?
    PHP 中的文本截取分析之效率
    FastAdmin 升级后出现 is already in use
    FastAdmin 环境变量 env 配置
    Nginx 服务器伪静态配置不当造成 Access denied
    笔记:明确认识
    进程通信(socket)
    进程间通信(了解)
    c++ 继承,组合
    c++ 类初始化
  • 原文地址:https://www.cnblogs.com/zstu-abc/p/3348662.html
Copyright © 2011-2022 走看看