Find The Multiple - POJ 1426 - Virtual Judge (vjudge.net)
题意:
给一个n,求一个能被它整除的数只由01组成
思路:
宽搜,先放进去一个1,然后每次把所有的添进去,在后面加一个1或者0,然后有符合要求的就输出并退出。
#include <iostream>
#include <queue>
#define int long long
using namespace std;
const int N = 1e3 + 10;
int n;
void bfs()
{
queue<int> q;
q.push(1);
while (!q.empty())
{
int t = q.front();
q.pop();
int a = t * 10;
int b = a + 1;
if (a % n ==0)
{
cout << a << endl;
break;
}
if (b % n == 0)
{
cout << b << endl;
break;
}
q.push(a);
q.push(b);
}
}
signed main()
{
while (cin >> n)
{
if (n == 0) break;
bfs();
}
return 0;
}