题意:输入一个数求其倍数(由01组成)。
广搜的初学者,感觉这个很容易理解。因为在搜索的过程中可能会出现较大的数,所以开long long型的队列。
#include<iostream>
#include<queue>
using namespace std;
void bfs(int n)
{
queue<long long>q;
q.push(1);
long long x ;
while(!q.empty()){ //广搜的套路
x = q.front();
q.pop(); //出队列是为了遍历后来入队列的可能
if(x % n == 0){
cout<<x<<endl;
return ;
}
q.push(x*10);
q.push(x*10 + 1);
}
}
int main()
{
int n;
while(cin>>n,n){
bfs(n);
}
return 0;
}