POJ.1426 Find The Multiple (BFS)

题意分析

给出一个数字n,求出一个由01组成的十进制数,并且是n的倍数。

思路就是从1开始,枚举下一位,因为下一位只能是0或1,故这个数字只能是1 * 10或者1 * 10 + 1。就按照这种方式枚举,依次放入队列,如果是其的倍数,就输出。

一开始没理解题意,以为是找一个能整除的二进制数,错了半天。

代码总览

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
queue<long long> q;
long long n,ans;
void bfs()
{
while(!q.empty()) q.pop();
q.push(1);
long long temp = 0;
while(!q.empty()){
temp = q.front();q.pop();
if(temp%n == 0){
ans = temp;
return;
}
q.push(temp*10);
q.push(temp*10+1);
}
}
int main()
{
while(scanf("%I64d",&n) != EOF && n!=0){
bfs();
printf("%I64d\n",ans);
}
return 0;
}

POJ.1426 Find The Multiple (BFS)的更多相关文章

  1. poj 1426 Find The Multiple( bfs )

    题目:http://poj.org/problem?id=1426 题意:输入一个数,输出这个数的整数 倍,且只有0和1组成 程序里写错了一个数,结果一直MLE.…… #include <ios ...

  2. 题解报告:poj 1426 Find The Multiple(bfs、dfs)

    Description Given a positive integer n, write a program to find out a nonzero multiple m of n whose ...

  3. POJ 1426 Find The Multiple(寻找倍数)

    POJ 1426 Find The Multiple(寻找倍数) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 Given ...

  4. POJ 1426 Find The Multiple (DFS / BFS)

    题目链接:id=1426">Find The Multiple 解析:直接从前往后搜.设当前数为k用long long保存,则下一个数不是k*10就是k*10+1 AC代码: /* D ...

  5. POJ - 1426 Find The Multiple(搜索+数论)

    转载自:優YoU  http://user.qzone.qq.com/289065406/blog/1303946967 以下内容属于以上这位dalao http://poj.org/problem? ...

  6. POJ 1426 Find The Multiple(数论——中国同余定理)

    题目链接: http://poj.org/problem?id=1426 Description Given a positive integer n, write a program to find ...

  7. 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 ...

  8. 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 ...

  9. POJ 1426 - Find The Multiple - [DP][BFS]

    题目链接:http://poj.org/problem?id=1426 Given a positive integer n, write a program to find out a nonzer ...

随机推荐

  1. 「Leetcode」13. Roman to Integer(Java)

    分析 把具体的情况一个一个实现即可,没有什么幺蛾子. 代码 class Solution { public int romanToInt(String s) { int ans = 0; for (i ...

  2. 【转】在Android Studio中下载Android SDK的两种方式(Android Studio3.0、windows)

    在Android Studio中下载Android SDK的两种方式(Android Studio3.0.windows) 方式一.设置HTTP Proxy1. 打开Settings2. 点击HTTP ...

  3. 关于scrum敏捷测试

    关于scrum的一些定义 敏捷软件开发方法是一种把新增功能通过较小的循环逐步迭代添加到项目中(的项目管理方法),工作是由自我组织的团队以高效合作的方式拥抱和适应变化来保证客户需求被真正满足的方式来完成 ...

  4. Python之requests的安装

    在 windows 系统下,只需要输入命令 pip install requests ,即可安装. 在 linux 系统下,只需要输入命令 sudo pip install requests ,即可安 ...

  5. 【ZABBIX】Zabbix触发器的告警原理及创建方法

    概述:       触发器中的表达式使用很灵活,我们可以创建一个复杂的逻辑测试监控,触发器表达式形式如下: {<server>:<key>.<function>(& ...

  6. 技本功丨收藏!斜杠青年与你共探微信小程序云开发(下篇)

    2019年2月26日,人们为了一个杯子疯了一天. 星巴克猫爪杯,一场已经与猫无关了的“圣杯战争“.网上的倒卖价格,已炒至近千元! 求而不得,舍而不能,得而不惜.这是人最大的悲哀... 所以,请珍惜以下 ...

  7. LearnPython - Zip格式文件的解压缩

    import zipfile import os def unzip(zip_name, target_dir): files = zipfile.ZipFile(zip_name) for zip_ ...

  8. Hibernate入门篇<1>hibernate.cfg.xml学习小结

    Hibernate配置文件主要用于配置数据库连接和Hibernate运行时所需的各种属性,这个配置文件应该位于应用程序或Web程序的类文件夹 classes中.Hibernate配置文件支持两种形式, ...

  9. ES6的新特性(12)——Set 和 Map 数据结构

    Set 和 Map 数据结构 Set 基本用法 ES6 提供了新的数据结构 Set.它类似于数组,但是成员的值都是唯一的,没有重复的值. Set 本身是一个构造函数,用来生成 Set 数据结构. co ...

  10. B. Counting-out Rhyme(约瑟夫环)

    Description n children are standing in a circle and playing the counting-out game. Children are numb ...