题目描述:
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.
输入描述:
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.
输出描述:
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.
输入:
2
6
19
0
输出:
10
100100100100100100
111111111111111111
题意:
在2100年科学家发现了平行宇宙,但是新发现的Earth2的世界中所有数字都是由0和1组成的十进制数,如果从我们的世界穿越到Earth2,数字将发生一些变化,例如:一个正整数n,将被转化为n的一个非零的倍数m,这个m应当符合Earth2的数字规则。你可以假定n不大于200且m不多于100位。
题解:
BFS水过去的
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
using namespace std;
int n;
typedef long long ll;
void bfs(ll x){
queue<ll>q;
q.push(1);
while(!q.empty()){
ll tmp = q.front();
q.pop();
if(tmp % (ll)x == 0){
printf("%lld\n",tmp);
return ;
}
q.push(tmp * 10);
q.push(tmp * 10 + 1);
}
}
int main(){
while(scanf("%d",&n)!=EOF){
if(n == 0) break;
bfs(n);
}
return 0;
}