给定一个整数,寻找一个只有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();
}

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-27
  • 2019-07-12
  • 2021-10-31
  • 2021-07-04
猜你喜欢
  • 2021-05-17
  • 2021-08-02
  • 2022-12-23
  • 2021-10-16
  • 2021-06-18
  • 2021-05-21
  • 2021-08-22
相关资源
相似解决方案