zoukankan      html  css  js  c++  java
  • POJ 1426 Find The Multiple(BFS)

    题目链接:http://poj.org/problem?id=1426

    题意:

      给一个数N,求一个仅由’0‘、’1‘组成的十进制数M使得 N|M(M能被N整除).解肯定有多组,只要求输出其中一个就OK.

    思路:

      暴力、绝对的暴力,但是要怎么暴力呢,还是BFS暴力的优雅点.以’1‘为树根,’10‘和’11‘为其子节点,'10'的自己子节点为’100‘、’101‘........依次类推,建立出一颗完全二叉树,然后对这棵解答树进行DFS或者BFS搜到答案就好了~

    代码:

     1 #include <iostream>
     2 #include <cmath>
     3 #include <cstdio>
     4 #include <cstring>
     5 #include <cstdlib>
     6 #include <stack>
     7 #include <queue>
     8 #include <vector>
     9 #include <algorithm>
    10 #include <string>
    11 
    12 typedef long long LL;
    13 typedef unsigned long long ULL;
    14 using namespace std;
    15 ULL n, ans;
    16 
    17 void BFS(int st){
    18     queue<ULL> Qu;
    19     Qu.push(1);//从‘1’开始
    20     while(!Qu.empty()) {
    21         ans = Qu.front();
    22         Qu.pop();
    23         if(ans % n == 0) return;//找到解
    24         for(int i = 0; i <= 1; i++) {
    25             ULL nex = ans * 10 + i;
    26             Qu.push(nex);
    27         }
    28     }
    29 }
    30 
    31 int main() {
    32     while(scanf("%llu", &n) == 1 && n != 0){
    33         ans = -1;
    34         BFS(1);
    35         printf("%llu
    ", ans);
    36     }
    37     return 0;
    38 }
  • 相关阅读:
    团队开发第二阶段
    每日日报
    每日日报
    每日日报
    每日日报
    每日日报
    C++类class和结构体struct区别
    c++简单的类的建立与参数的两种传递方法
    C++ 使用delete删除指针
    暂存
  • 原文地址:https://www.cnblogs.com/Ash-ly/p/5733593.html
Copyright © 2011-2022 走看看