题目:
在2100年科学家发现了平行宇宙,但是新发现的Earth2的世界中所有数字都是由0和1组成的十进制数,如果从我们的世界穿越到Earth2,数字将发生一些变化,例如:一个正整数n,将被转化为n的一个非零的倍数m,这个m应当符合Earth2的数字规则。你可以假定n不大于200且m不多于100位。
提示:本题采用Special Judge,你无需输出所有符合条件的m,你只需要输出任一符合条件的m即可。
Input
输入包含多组数据,每组数据仅一行,只包含一个正整数n,n==0时输入结束 (1 <= n <= 200).
Output
对于输入的每组n,都输出任一符合条件的m。即使有多个符合条件的m,你也只需要输出一个即可。
(已知long long范围内存在表示数据中所有的m
Sample Input
2
6
19
0
Sample Output
10
100100100100100100
111111111111111111
就是给定一个数,找到一个只由0,1组成的数并且可以整除给定的数 思路是搜索,操作就有两个,一个是*10,另个是*10+1,这道题深搜广搜(但是我的广搜超时了)
DFS
#include<iostream>
using namespace std;
int n ;
int sum=0;
void dfs(long long num ,int k ) {
if(sum) return;
if(num%n==0){
cout<<num<<endl;
sum = 1;
return;
}
if(k>=19) return;//题目数据限制
dfs(num*10,k+1);
dfs(num*10+1,k+1);
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
while (cin >> n) {
if(n==0) break;
sum = 0;
dfs(1,1);
}
return 0;
}*/
BFS超时
#include<iostream>
#include<queue>
using namespace std;
int n;
void bfs(long long x){
queue< long long > q;
q.push(1);
while(!q.empty()){
long long temp = q.front();
q.pop();
if(temp%(long long) x == 0){
printf("long long d\n",temp);
return;
}
q.push(temp*10);
q.push(temp*10+1);
}
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
while(scanf("%d",&n)!=EOF){
if(n==0) break;
bfs(n);
}
return 0;
}