poj - 1426 - Find The Multiple

本文提供了一种使用广度优先搜索(BFS)解决 POJ 1426 的方法,该题要求找到 n 的倍数 m,且 m 的十进制表示仅包含 0 和 1。提供了两种解法:一种利用 BFS,另一种通过将二进制数转换为十进制数来寻找解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意:求n的一个倍数m,m的十进制表示的每一位要不为0,要么为1。(1 <= n <= 200)

题目链接:http://poj.org/problem?id=1426

——>>这题的数据可能不是那么的严,可以用long long水过。

寒假时bfs过

#include <iostream>
#include <queue>

using namespace std;

void bfs(int n, long long &s)
{
    queue<long long> qu;
    qu.push(1);
    while(!qu.empty())
    {
        s = qu.front();
        qu.pop();
        if(!(s%n)) return;
        else
        {
            qu.push(s*10);
            qu.push(s*10+1);
        }
    }
    while(!qu.empty())qu.pop();
}
int main()
{
    int n;
    while(cin>>n)
    {
        if(!n) return 0;
        long long s;
        bfs(n, s);
        cout<<s<<endl;
    }
}

今天暴搜过:

translate(x)将十进制数x的二进制表示以十进制数表示出来。

#include <cstdio>

using namespace std;

const int maxn = 100 + 10;

long long translate(int x)
{
    int i, j, buf[maxn];
    for(i = 0; x; i++)
    {
        buf[i] = x % 2;
        x /= 2;
    }
    long long ret = 0;
    for(j = i-1; j >= 0; j--) ret = ret * 10 + buf[j];
    return ret;
}
int main()
{
    int n, i;
    long long temp;
    while(scanf("%d", &n) == 1 && n)
    {
        for(i = 1; ; i++)
        {
            temp = translate(i);
            if(temp % n == 0) break;
        }
        printf("%I64d\n", temp);
    }
    return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值