POJ 1426.Find The Multiple(bfs)(c++)
题目链接:Find The Multiple 题目大意: 给出一个整数n,(1 <= n <= 200)。求出任意一个它的倍数m,要求m必须只由十进制的’0’或’1’组成。(具体细节请看原题目) 样例:
Sample Input
2
6
19
0
Sample Output
10
100100100100100100
111111111111111111
题解: 网上有许多高级的解释和算法,运用有关余数方面的数学知识。但是题目给定的数据用bfs可以ac,所以就直接bfs就可以。
代码如下:
#include<iostream>
#include<queue>
using namespace std
;
queue
<long long> q
;
long long bfs(int n
)
{
long long start
= 1;
q
.push(start
);
while (!q
.empty()) {
start
= q
.front();
q
.pop();
if (start
% n
== 0) {
return start
;
}
q
.push(start
* 10);
q
.push(start
* 10 + 1);
}
}
int main()
{
int n
;
while (cin
>> n
&& n
) {
while (!q
.empty()) {
q
.pop();
}
cout
<< bfs(n
) << endl
;
}
}