Find The Multiple POJ - 1426 (BFS)

题目大意

给定一个整数,寻找一个只有0,1构成的十进制数使得这个数能够整除这个整数

解法

直接bfs第一位放入1,之后每一位放入1或者0

代码

#include <iostream>
#include <queue>
using namespace std;
int n;
void bfs()
{
  queue<long long> q;
  q.push(1);
  while(q.size())
  {
    long long p=q.front();
    q.pop();
    if(p%n==0)
    {
      cout<<p<<"\n";
      return;
    }
    q.push(p*10);
    q.push(p*10+1);
  }
}
int main()
{
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  while(cin>>n&&n)
  bfs();
}
posted @ 2018-12-11 18:50  baccano!  阅读(131)  评论(0编辑  收藏  举报