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 }

 

转载于:https://www.cnblogs.com/Nautilus1s/p/5971661.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值