poj1426 Find The Multiple--BFS

题目来源:poj 1426

题意:找出被n整除的任意一个每一位中只有1和0的数
方法:直接bfs从小到大按位遍历只有1和0的数。

注意:

  1. 好像是STL Queue做的用G++交才不会报错
  2. 数据比较大,要定义unsigned long long类型来存储。
#include <iostream>
#include <queue>
using namespace std;
typedef unsigned long long ll;
ll n;
queue<ll> Q;
void BFS(ll x)
{
    while (!Q.empty())
    {
        Q.pop();
    }
    Q.push(1ll);
    while (!Q.empty())
    {
        ll top = Q.front();
        if (top % x == 0)
        {
            cout << top << endl;
            return;
        }
        Q.pop();
        Q.push(top * 10);
        Q.push(top * 10 + 1);
    }
}

int main()
{

    while (cin >> n && n)
    {
        BFS(n);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_39504764/article/details/89880970