POJ 1426 Find The Multiple(深搜)
题意:给定一个正整数n,编写一个程序来找出n的非零倍数m,它的十进制表示只包含数字0和1。您可以假设n不大于200,并且对应的m不包含100小数位数。
典型的广搜思路超时了。。。。
于是改用深搜+一个记忆数组
solution:
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
bool flag;
unsigned long long int s[201], tot;
void dfs(unsigned long long int x,int n,int c){
if(s[n]){
tot = s[n];
flag = true;
return;
}
if(flag)return;
if(x % n == 0){
tot = x;
s[n] = tot;
flag = true;
return ;
}
if(c == 19)return ;//long long的范围
dfs(x * 10, n, c + 1);
dfs(x * 10 + 1, n, c + 1);
}
int main(){
int n;
while(cin >> n && n){
flag = false;
dfs(1, n, 1);
cout << tot << endl;
}
}