Find the Multiple POJ1426

题目链接:Find the Multiple

题目大意

找出一个只由0和1组成的能整除n的数。

思路

所有由0和1组成的数可以看作是某个只由0、1组成的数a经过以下两种变化得到

  1、a * 10

  2、a * 10+1

因此只需要用BFS搜索满足条件的数即可。

题解

 1 #include <iostream>
 2 #include <cstring>
 3 #include <queue>
 4 
 5 using namespace std;
 6 
 7 long long n;
 8 int main(int argc, char const *argv[])
 9 {
10     while(cin >> n && n)
11     {
12         queue<long long> q;
13         q.push(1);
14         while(!q.empty())
15         {
16             if(q.front() % n == 0)
17             {
18                 cout << q.front() << endl;
19                 break;
20             }
21             q.push(q.front()*10);
22             q.push(q.front()*10+1);
23             q.pop();
24         }
25     }
26     return 0;
27 }
原文地址:https://www.cnblogs.com/SaltyFishQF/p/10298984.html