首页 > 其他 > 详细

poj1426 宽搜

时间:2020-03-25 12:34:58      阅读:53      评论:0      收藏:0      [点我收藏+]
Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains only 
the digits 0 and 1. You may assume that n is not greater than 200 and there is a corresponding m containing no more than 100
decimal digits.

考试的时候一看200位直接就被吓到了,直接跳过,后来听说答案开long long 可过,就是普通宽搜,感觉亏了一个亿,既然每一位不是0就是1,首先第一位一定是1,然后每次这个数乘10,或者乘10加1,就是可以产生每一位都是1或者0的数字了。

代码:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<queue>
 4 #define ll long long
 5 using namespace std;
 6 int n;
 7 queue<long long>q;
 8 ll ans;
 9 int main()
10 {
11     while(1)
12     {
13         cin>>n;
14         if(!n)break;
15         while(!q.empty())q.pop();
16         q.push(1);
17         while(!q.empty())
18         {
19             ll k=q.front();q.pop();
20             if(k%n==0)
21             {
22                 ans=k;break;
23             }
24             q.push(k*10),q.push(k*10+1);
25         }
26         printf("%lld\n",ans);
27     }
28 
29     return 0;
30 }

 

poj1426 宽搜

原文:https://www.cnblogs.com/yuelian/p/12565294.html

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