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.
这道题的大意是这样的:找到一个由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或不加1
search(a*10+i,s+1);
}
}
}
int main(){
while(cin>>b&&b!=0){
search(1,0);
cout<<ans<<endl;
f=0;
ans=0;
}