Find the Multiple

来源:互联网 发布:蓝月羽毛升级数据 编辑:程序博客网 时间:2024/06/06 20:19
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.
Input
The input file may contain multiple test cases. Each line contains a value of n (1 <= n <= 200). A line containing a zero terminates the input.
Output
For each value of n in the input print a line containing the corresponding value of m. The decimal representation of m must not contain more than 100 digits. If there are multiple solutions for a given value of n, any one of them is acceptable.
Sample Input
2619

0这道题的大意是这样的:找到一个由0和1组成的十进制数字,使得这个数是所输入的数的倍数。

这道题呢其实是在吓人,它说是一百位,但实际上开个long long 就可以过了,看的就是你敢不敢去是。

知道题就是一个深搜的题,因为这个是0 1 串,所以我们从1开始搜,往10*k和10*k+1方向搜,保证能搜到全部 01 串,但是我们也要有个边界条件,那就是到了20位或者已经搜到了符合条件的数,那么我们就跳出。

#include<iostream> using namespace std;int b;unsigned long long ans;bool f;void search(unsigned long long a,int s){if (f) return;if (s >= 19) return;if(a%b==0){ans = a;f = 1;return;}else {for(int i=0;i<2;i++){//两个方向乘10加1或不加1search(a*10+i,s+1);}}} int main(){while(cin>>b&&b!=0){search(1,0);cout<<ans<<endl;f=0;ans=0;}

原创粉丝点击