zoukankan      html  css  js  c++  java
  • POJ 1426 搜索进阶

    这道题我认为还是比较难的,题意就不是很好理解(英语太渣),然后看了样例整个人就懵逼了,看了一个大神的题解递归懵逼了有木有!!!

    后来好好想一下还是搜索,假定一位后,搜索下一位情况,这里下一位要么是0,要么是1,我把它叫做二重搜索哈哈哈哈哈

    题解如下(●'◡'●)

    题意:给出一个数n,找到一个数m只由0,1组成,使得m是n的倍数;

    例如n=6

    第一位肯定要置1,1%6 = 1;

    10 %6 = 4

    11%6 = 5;

    100%6 = 4;1000%6 = 4;1001%6 = 5;

    101 %6 = 5;1010 %6 = 2;1011%6 = 3;

    110 %6 = 2;1100 %6 = 2;1101%6 = 3;

    111 %6 = 3;1110%6 = 0;宽搜结束

    dfs代码

     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <stdlib.h>
     4 #include <math.h>
     5 #include <queue>
     6 #define MAXN 300005
     7 using namespace std;
     8 int flag;
     9 void dfs(long long int last,int n,int k)
    10 {
    11     if(flag)
    12         return;
    13     if(last%n == 0)
    14     {
    15         flag = 1;
    16         printf("%lld
    ",last);
    17         return ;
    18     }
    19     if(k == 19)
    20         return;
    21     dfs(last*10,n,k+1);
    22     dfs(last*10+1,n,k+1);
    23 }
    24 int main()
    25 {
    26     freopen("caicai.txt","r",stdin);
    27     int n;
    28     while(~scanf("%d",&n) && n)
    29     {
    30         flag = 0;
    31         dfs(1,n,1);
    32     }
    33     return 0;
    34 }
    View Code

    bfs

    例如n=6

    第一位肯定要置1,1%6 = 1;

    10 %6 = 4

    11%6 = 5;

    100%6 = 4;1000%6 = 4;1001%6 = 5;

    101 %6 = 5;1010 %6 = 2;1011%6 = 3;

    110 %6 = 2;1100 %6 = 2;1101%6 = 3;

    111 %6 = 3;1110%6 = 0;宽搜结束

    代码

     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <stdlib.h>
     4 #include <math.h>
     5 #include <queue>
     6 #define MAXN 300005
     7 using namespace std;
     8 int ans[MAXN];
     9 
    10 int bfs(int n)
    11 {
    12     queue<long long> q;
    13     q.push(1);
    14     while(!q.empty())
    15     {
    16         long long x = q.front();
    17         q.pop();
    18         if(x%n==0)
    19         {
    20             printf("%lld
    ",x);
    21             return 0;
    22         }
    23         q.push(x*10);
    24         q.push(x*10+1);
    25     }
    26 }
    27 
    28 int main()
    29 {
    30     //freopen("caicai.txt","r",stdin);
    31     int n;
    32     while(~scanf("%d",&n) && n)
    33     {
    34         bfs(n);
    35     }
    36     return 0;
    37 }
    View Code

    明天再补。。。

    #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <queue>
    #define MAXN 300005
    using namespace std;
    int ans[MAXN];
    
    int bfs(int n)
    {
        queue<long long>q;
        q.push(1);
        while(!q.empty())
        {
            long long x = q.front();
            q.pop();
            if(x%n==0)
            {
                printf("%lld
    ",x);
                return 0;
            }
            q.push(x*10);
            q.push(x*10+1);
        }
    }
    
    int main()
    {
        //freopen("caicai.txt","r",stdin);
        int n;
        while(~scanf("%d",&n) && n)
        {
            bfs(n);
        }
        return 0;
    }
    
  • 相关阅读:
    Shell 基本运算符
    如何将mongo查询结果导出到文件中
    python如何将自己写的代码打包供他人使用
    json格式化
    如何在linux安装ruby2.2.2+
    如何对字符串进行码表替换转码--加密encode
    git基础
    如何创建redis集群
    linux安装python2.7
    如何在windows和linux搭建django环境
  • 原文地址:https://www.cnblogs.com/caitian/p/5419241.html
Copyright © 2011-2022 走看看