【POJ1426】Find The Multiple

时间:2019-09-21
本文章向大家介绍【POJ1426】Find The Multiple,主要包括【POJ1426】Find The Multiple使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

本题传送门

本题知识点:深度优先搜索 | 宽度优先搜索

题意很简单,让我们找一个只有1和0组成的十位数是n的倍数的数。

这题一开始吓到我了——因为Output里说输出的长度最长不超过100位???那是不是要用字符串了?不过貌似在[1, 200]里,他们的倍数好像都在18位内,即用unsigned long long就可以解决。(BUG?)

若想学习输出100位的可以看看这篇博客

数据很小。

// POJ 1426
#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;

int n, m;
bool take;
struct node{
    unsigned long long num;
    int len;
};
queue<node> que;

void dfs(unsigned long long ans, int t){
    if(take) return ;
    if(ans % n == 0){
        printf("%llu\n", ans);
        take = true;
        return ;
    }
    if(t == 19) return ;

    dfs(ans * 10, t + 1);
    dfs(ans * 10 + 1, t + 1);
}

void bfs(){
    node a; a.num = 1; a.len = 0;
    que.push(a);

    while(!que.empty()){
        node now = que.front(), next; que.pop();
        if(now.num % n == 0){
            printf("%llu\n", now.num);
            break;
        }

        if(now.len == 19) continue;
        else {
            next.num = now.num * 10; next.len = now.len + 1;
            que.push(next);
            next.num++;
            que.push(next);
        }
    }
}

int main()
{
    while(~scanf("%d", &n) && n){
        while(!que.empty()) que.pop();
        take = false;
//        dfs(1, 0);
        bfs();
    }

//    freopen("Find The Multiple.txt", "w", stdout);
//    for(n = 1; n <= 200; n++){
//        take = false;
//        dfs(1, 0);
//    }
    return 0;
}

原文地址:https://www.cnblogs.com/Ayanowww/p/11561450.html