POJ1426 Find The Multiple题意及题解

POJ1426-Find The Multiple题解与BFS+同余模算法实现 POJ 1426 是一个典型的图论问题,该问题要求解决者根据给定的条件,使用有效的算法来找出最短路径。此问题作为算法和数据结构训练的基础题目之一,对于深入理解搜索算法及其应用场景具有重要意义。Breadth-First Search(BFS)是一种用于图的搜索算法,也称为宽度优先搜索。它从图中的某一节点开始,首先访问所有相邻的节点,然后对每一个相邻节点进行同样的操作,直到所有节点都被访问过。这种策略使得BFS能够均匀地向外扩展,直到找到目标节点或搜索完整个图。 阅读详情
Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains only the digits 0 and 1. You may assume that n is not greater than 200 and there is a corresponding m containing no more than 100 decimal digits.
Input
The input file may contain multiple test cases. Each line contains a value of n (1 <= n <= 200). A line containing a zero terminates the input.
Output
For each value of n in the input print a line containing the corresponding value of m. The decimal representation of m must not contain more than 100 digits. If there are multiple solutions for a given value of n, any one of them is acceptable.
Sample Input
2
6
19
0
Sample Output
10
100100100100100100
111111111111111111

题意为找到一个大于等于n的倍数m,且m只由0和1组成。暴力会超时,所以从1开始直接列举所有的二进制表示。用bfs分两种情况,一种是当前数乘10,另一种是当前数乘10加1,因为n最大为200,故列举二进制不会超时。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
using namespace std;
void bfs(int n)
{
    queue<long long> q;//q的数会比较大,所以用long long
    while(!q.empty())
        q.pop();
    q.push(1);
    while(1)
    {
        long long now=q.front();
        if(now%n==0)
        {
            cout<<now<<endl;
            return ;
        }
        q.pop();
        q.push(now*10);
        q.push(now*10+1);
    }
}
int main()
{
    int n;
    while(cin>>n&&n)
    {
        bfs(n);
    }
    return 0;
}




北大POJ初级-所有题目AC代码+解题报告 立即下载

相关推荐

PCIe网卡驱动实现分析(三)--- cache一致性

在现代处理器系统中,CPU的主频远高于主存读写速度,主存的读写速度成为程序执行效率的瓶颈;为了解决这个问题,在CPU和主存之间引入了Cache存储器,基于程序执行的局部性原理,在程序执行时,将正在使用的数据从主存拷贝一份到Cache,这样CPU就可以直接从Cache读写数据以改善主存读写速度慢的问题;

a935854743的博客 1776

Given a positive integer n....

Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains only the digits 0 and 1. You may assume that n is not greater than 200 and there...

大魚的个人博客,如有不足,欢迎指正。 2464

PyCharm旧版本下载地址

快捷找到PyCharm历史版本下载地址

piaopiaolanghua的专栏 4429

poj1426(dfs)

Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains only the digits 0 and 1. You may assume that n is not greater than 200 and there...

weixin_43965698的博客 332

基于PLC智能立体车库控制系统设计

摘要 本系统设计以三层立体车库为研究对象,通过对立体车库的原理分析、动作流程设计和一系列的软硬件设计,达到立体车库自动化控制的目的。系统采用可编程控制器作为立体车库控制系统的控制单元,实现该系统的工艺要求,通过程序仿真调试,完成系统的各种动作要求,达到自动控制的目的。系统设计分为总体设计、硬件设计以及软件设计。按照立体车库控制系统的控制策略进行图纸设计;重点分析工艺流程图,对立体车库控制系统进行程序编译和MCGS上位机调试,通过系统的调试和修改,达到了基本控制的要求。系统设计明确了工艺要求,确定

DX884164131的博客 3600

台球 PCOL:极致物理还原的网页斯诺克引擎(附源码深度解析)

*完整源码地址**:[https://github.com/billiards/pcol-core](https://github.com/billiards/pcol-core)(包含详细文档与示例)通过这个项目,我们不仅创建了一个游戏,更构建了一个**可扩展的物理引擎框架**,为网页端复杂物理模拟开辟了新的可能性。**在线体验**:[https://pcol.billiards](https://pcol.billiards)(支持移动端触控操作)clone.velocity.x *= -0.92;

Loving_enjoy的博客 2796

POJ-1426-Find The Multiple(DFS)

Description Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains only the digits 0 and 1. You may assume that n is not greater tha

好吗好的~的技术博客 649

【补充篇】三万字了解AUTOSAR软件架构(一)

主要讲解了AUTOSAR软件架构的分层及各个栈的作用

weixin_47571898的博客 1117

【简单搜索】 POJ 1426 Find the Multiple详解

Find The Multiple: Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains only the digits 0 and 1. You may assume that n is not greater than 200 and there is a corresponding m containing no

Qiuker_jl的博客 517

三菱FX5U系列PLC与汇川IT6000系列触摸屏进行MODBUS TCP通信的具体方法

三菱FX5U系列PLC与汇川IT6000系列触摸屏进行MODBUS TCP通信的具体方法

Robot_PLC_HMI_Automation 6514

RK3588 MPP,RGA,DRM,QT之RGA

RK3588多路视频采用RGA来进行混合成一帧

dreamliweiming的博客 4265

POJ-1426

Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains only the digits 0 and 1. You may assume that n is not greater than 200 and there...

qq_42964711的博客 852

Transformer大模型实战 文本摘要任务

Transformer, 文本摘要, 自然语言处理, 预训练模型, 序列到序列, BERT, BART, T5 1. 背景介绍 文本摘要任务是自然语言处理 (NLP) 领域的重要研究方向之一,旨在从长篇文本中提取关键信息,生成简洁、准确的摘要。随着深度学习技术的快速发展,基于 Tra

AI大模型应用之禅 1339

【poj 1426】Find The Multiple 题意&题解&代码(C++)

poj

曾经追梦的happy时光 1081

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

POJ 1426 Find The Multiple 简单dfs构造

POJ 1426 Find The Multiple 简单dfs构造题意大概意思题解 链接: 原题网站. 题意 Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains only the digits 0 and 1. You may assume that n is not greater than 200 and there

zkpj12的博客 262

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

POJ1426--Find The Multiple

POJ1426–Find The Multiple 目录POJ1426--Find The Multiple一、题目链接二、题目内容三、题意拆解四、解题思路五、参考代码 一、题目链接 POJ1426 二、题目内容 Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains only the digits 0 and 1. You

Verber的博客 792

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

POJ 1426 Find The Multiple

POJ 1426 Find The Multiple 题目大意: 给出一个整数n,(1 <= n <= 200)。求出任意一个它的倍数m,要求m必须只由十进制的’0’或’1’组成。 具体思路: 这道题,如果真的十进制位数不超过100位,只要超过unsigned long long的范围,加上处理大数 蒟蒻的我没有思路。但是这个题目的测试数据很小,unsigned long long可以...

指弹键盘哼民谣 739

POJ-1426 Find The Multiple

状压

Brust Link 930

poj 1426 Find The Multiple (bfs 搜索)

Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18012   Accepted: 7297   Special Judge Description Given a positive integer n, write a pr

Just do it ! 1787

E - Find The Multiple POJ - 1426

E - Find The MultiplePOJ - 1426 Find The Multiple Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains only the digits 0 and 1. ...

qq_31267769的博客 283

POJ-1426 Find The Multiple

POJ-1426 Find The Multiple 题目链接:POJ-1426 题目大意:找到一个数 这个数只由1或0组成 且是给定数的整倍数 解题思路:只由1或者0组成 那么就从1为起点 每次乘10 或者乘10+1 就能覆盖到所有的数 BFS搜索即可 注意示例给定的不是最小解 不要被误导 代码块: #include<iostream> #include<queue> ...

鱼头的水题之路 215

【POJ】1426 Find The Multiple

题目链接:http://poj.org/problem?id=1426 题意:给定一个正整数n,找一个比n大且能只由01构成的且能够被n整除的数。 题解:这个就是在后面添0和添1小心试探。一定要是添0不成功再去添1。 代码: 1 #include<iostream> 2 #include<cstdio> 3 using na...

deze1991的专栏 170

poj1426 Find The Multiple--BFS

题目来源:poj 1426 题意:找出被n整除的任意一个每一位中只有1和0的数 方法:直接bfs从小到大按位遍历只有1和0的数。 注意: 好像是STL Queue做的用G++交才不会报错 数据比较大,要定义unsigned long long类型来存储。 #include <iostream> #include <queue> using namespace std...

圣托里尼的日落啊~的博客 275

POJ1426 Find The Multiple —— BFS

题目链接:http://poj.org/problem?id=1426 Find The Multiple Time Limit:1000MS Memory Limit:10000K Total Submissions:34218 Accepted:14337 Special Judge ...

alince20008的博客 136
上一篇: PAT甲级1001. A+B Format
下一篇: c++实现四舍五入问题
Yolo_Fs
博客等级 码龄9年 41粉丝 46原创
评论 5
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值