POJ 1426 Find The Multiple (BFS基础)

题目大意:

  就是说,给你一个数字n,求出任意一个比这个n大的数字,并且这个数字是n的倍数,且只能由0和1组成。

解题思路:

  刚开始考虑了大数问题,但是仔细想了下,是多虑的,因为就题目的样例来看,给你的这几个答案都很长。。。实际有比这个答案更小的数字。

双入口的BFS,只要一次向队列中进入两个元素就好了,q.push(x*10), q.push(x*10+1);

代码:

  

 1 # include<cstdio>
 2 # include<iostream>
 3 # include<queue>
 4 
 5 using namespace std;
 6 
 7 int n;
 8 
 9 void bfs ( int x )
10 {
11     queue<long long>que;
12     que.push(1);
13     while ( !que.empty() )
14     {
15         long long xx = que.front();
16         que.pop();
17         if ( xx%x==0 )
18         {
19             printf("%lld
",xx);
20             return;
21         }
22         que.push(10*xx);
23         que.push(10*xx+1);
24     }
25 }
26 
27 int main(void)
28 {
29 
30     while ( cin>>n )
31     {
32         if ( n==0 )
33             break;
34         bfs(n);
35     }
36 
37 
38     return 0;
39 }
原文地址:https://www.cnblogs.com/wikioibai/p/4419229.html