要求输出m能被n整除
为什么bfs最后用 if(next%n == 0) return next;反而不行
#include<iostream>
#include<queue>
#include<stdio.h>
#include<string.h>
using namespace std;
typedef long long ll;
int vis[210];
ll BFS(int n){
queue <ll> q;
ll now, next;
while(!q.empty()) q.pop();
q.push(1);
vis[1%n] = 1;
if (1%n == 0) return 1;//处理第一个数
while(!q.empty()){
now = q.front();
q.pop();
for(int i=0; i<2; ++i){
next = 10 * now + i;
if (next%n == 0) return next;
if (!vis[next%n]){
q.push(next);
vis[next%n] = 1;
}
}
}
return next;
//if(next%n == 0) return next;
}
int main(){
int n;
while(scanf("%d", &n) != EOF){
if (n == 0) break;
memset(vis, 0, sizeof(vis));
printf("%lld\n", BFS(n));
}
}