题面
给定一个正整数n,请编写一个程序来寻找n的一个非零的倍数m,这个m应当在十进制表示时每一位上只包含0或者1。你可以假定n不大于200且m不多于100位。
提示:本题采用Special Judge,你无需输出所有符合条件的m,你只需要输出任一符合条件的m即可。
Input
输入包含多组数据,每组数据仅一行,只包含一个正整数n (1 <= n <= 200).
Output
对于输入的每组n,都输出任一符合条件的m。即使有多个符合条件的m,你也只需要输出一个即可。
Sample Input
2
6
19
0
Sample Output
10
100100100100100100
111111111111111111
分析
m只包含0和1,那么最高位一定为1,从1开始不断在后面添加0和1得到两个新数m,直到m是倍数,由于在遍历顺序中,m的位数是逐渐增加的,可以用bfs
另外,在这里倍数m其实直接用long long 就足够了
代码
#include<iostream>
using namespace std ;
#include<queue>
#include<stdio.h>
typedef long long LL;
LL num;
void bfs(LL n)
{
queue<LL> que; //
que.push(1); //
while(1){
num=que.front();
que.pop();
if(num%n==0) return ;
que.push(num*10+1);
que.push(num*10);
}
}
int main()
{
LL n;
while(scanf("%lld",&n) && n) {
bfs(n);
printf("%lld\n",num);
}
return 0;
}