Find The Multiple(POJ1426)(入门)

本文介绍了一个使用广度优先搜索(BFS)算法解决特定数学问题的C++实现案例。该问题要求找到一个最小的正整数,通过在其末尾附加0或1的方式最终能被给定的整数n整除。文章提供的代码实现了这一算法,并通过循环不断生成新的数字直到找到符合条件的解。

どこでもドア:http://poj.org/problem?id=1426

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<sstream>
#include<set>
#include<cstdlib>
#include<map>
#include<queue>
using namespace std;
typedef long long LL;

LL BFS(int n)
{
    queue<LL> Q;
    Q.push(1);
    while(!Q.empty())
    {
        LL X = Q.front();
        Q.pop();
        if(X % n == 0)
            return X;
        Q.push(X * 10);
        Q.push(X * 10 + 1);
    }
    return 0;
}
int main()
{
    int n;
    while(cin>>n,n)
    {

        cout << BFS(n) << endl;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值