首页 > 其他 > 详细

POJ 1426 Find The Multiple (BFS基础)

时间:2015-04-12 11:59:45      阅读:82      评论:0      收藏:0      [点我收藏+]

题目大意:

  就是说,给你一个数字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\n",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 }

 

POJ 1426 Find The Multiple (BFS基础)

原文:http://www.cnblogs.com/wikioibai/p/4419229.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!