【题目链接】
http://poj.org/problem?id=1426
题目意思
给定一个n,找出一个为01组成的数,而且这个数还要是n倍数。答案可能多个,输出任意一个就可以。
解题思路
直接从一开始不断试就可以了,每次都等于x*10和x*10+1两种结果。有趣的是这题如果返回bfs值再输出就wa了,直接输出就过了。另外还有一种规律解,这里就不写了。
代码部分
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
#define LL unsigned long long
#define inf 0x3f3f3f3
const int N = 1e5+5;
int n;
void bfs(LL s)
{
queue<LL>q;
q.push(s);
while (!q.empty())
{
LL t = q.front();
q.pop();
if (t%n == 0)
{
printf("%lld\n",t);
return ;
}
q.push(t*10);
q.push(t*10+1);
}
}
int main()
{
while (cin>>n,n)
{
bfs(1);
}
return 0;
}