挺简单的BFS搜索,用DFS也能过,不过要注意搜索深度,不要越出了数据范围,大约是第19层会越出数据范围(DFS的话),不过啊、、、这个题,能想到用BFS 不容易啊,,用C++交超时,用G++就A了。尴尬查了一下这俩是编译上不同
//leehaoze
#include <iostream>
#include <deque>
#include <string>
#include <vector>
#include <queue>
#include <cstdio>
#include <stack>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <cstdio>
#include <cmath>
#include <cstdlib>
using namespace std;
const int INF = 1<<29;
#define INC_SAT( val ) (val = ((val)+1 > (val)) ? (val)+1 : (val))
#define ARR_SIZE( a ) ( sizeof( (a) ) / sizeof( (a[0]) ) )
#define ULL unsigned long long
int n;
long long k;
bool Input(){
scanf("%d",&n);
return n != 0;
}
void BFS(){
k = 1;
queue<long long> Q;
Q.push(k);
while(!Q.empty()){
long long now = Q.front();
Q.pop();
if(now % n == 0){
printf("%lld\n",now);
return ;
}
Q.push(now * 10);
Q.push(now * 10 + 1);
}
}
int main() {
#ifdef LOCAL
freopen("IN.txt", "r", stdin);
#endif
std::ios::sync_with_stdio(false);
while(Input()){
BFS();
}
}