POJ 1426 Find The Multiple

题意:给一个数N,找到一个N的倍数,其十进制只由0、1构成,输出其中一个就行。

分析:因为倍数只由0、1构成,故每次有两种方式移到下一状态:10N,10N+1。N最到位200故要用long long。

代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <vector>
 5 #include <queue>
 6 #include <cmath>
 7 #include <stack>
 8 #include <set>
 9 #include <map>
10 #include <algorithm>
11 using namespace std;
12 #define ll long long
13 #define inf 0x3f3f3f3f
14 int n;
15 void bfs()
16 {
17     queue<ll> q;
18     q.push(1);//遍历所有由1,0构成的数,从1开始
19     while(!q.empty())
20     {
21         ll a=q.front();
22         q.pop();
23         if(a%n==0)
24         {
25             printf("%lld\n",a);
26             break;
27         }
28         q.push(a*10);
29         q.push(a*10+1);
30     }
31 }
32 int main()
33 {
34     int i;
35     while(scanf("%d",&n),n)
36     {
37         bfs();
38     }
39     return 0;
40 }

 

posted @ 2016-10-17 23:09  Nautilus1s  阅读(140)  评论(0编辑  收藏  举报