Find The Multiple

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 20000/10000K (Java/Other)
Total Submission(s) : 43   Accepted Submission(s) : 21
Special Judge
Problem 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 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
 
Source
PKU

题意:构造一个十进制由0和1组成的整数m,让m能够被n整除;题目中给出了m是小于等于100位的。

思路:bfs可以做出的,只是100位这个条件让我很头疼,我先用string试了试交了之后是超时,后来看了看答案用long long也给过了,所以m肯定没有100位的;

string类型代码:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<queue>
  5.  
  6. using namespace std;
  7.  
  8. queue<string>s;
  9. int a,c;
  10. string b;
  11.  
  12. int chu(string s)
  13. {
  14. c=;
  15. for(int i=;i<s.length();i++){
  16. c*=;
  17. c+=s[i]-'';
  18. c%=a;
  19. }
  20. if(c==)
  21. return ;
  22. else
  23. return ;
  24. }
  25.  
  26. int bfs()
  27. {
  28. while(!s.empty()){
  29. b=s.front();
  30. s.pop();
  31. if(b.length()>)
  32. continue;
  33. if(chu(b+'')==){
  34. cout<<b+''<<endl;
  35. while(!s.empty()){
  36. s.pop();
  37. }
  38. return ;
  39. }
  40. else{
  41. s.push(b+'');
  42. }
  43.  
  44. if(chu(b+'')==){
  45. cout<<b+''<<endl;
  46. while(!s.empty()){
  47. s.pop();
  48. }
  49. return ;
  50. }
  51. else{
  52. s.push(b+'');
  53. }
  54. }
  55. return ;
  56. }
  57.  
  58. int main()
  59. {
  60. // freopen("input.txt","r",stdin);
  61. while(cin>>a){
  62. if(a==)
  63. break;
  64. else{
  65. s.push("");
  66. bfs();
  67. }
  68. }
  69. return ;
  70. }

long long类型代码:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<queue>
  5.  
  6. using namespace std;
  7.  
  8. queue<long long>s;
  9. int a,c;
  10. long long b;
  11.  
  12. int bfs()
  13. {
  14. while(!s.empty()){
  15. b=s.front();
  16. s.pop();
  17. if(b%a==){
  18. cout<<b<<endl;
  19. while(!s.empty()){
  20. s.pop();
  21. }
  22. return ;
  23. }
  24. else{
  25. s.push(b*);
  26. s.push(b*+);
  27. }
  28. }
  29. return ;
  30. }
  31.  
  32. int main()
  33. {
  34. // freopen("input.txt","r",stdin);
  35. while(cin>>a){
  36. if(a==)
  37. break;
  38. else{
  39. s.push();
  40. bfs();
  41. }
  42. }
  43. return ;
  44. }

Find The Multiple(bfs)的更多相关文章

  1. POJ.1426 Find The Multiple (BFS)

    POJ.1426 Find The Multiple (BFS) 题意分析 给出一个数字n,求出一个由01组成的十进制数,并且是n的倍数. 思路就是从1开始,枚举下一位,因为下一位只能是0或1,故这个 ...

  2. poj 1426 Find The Multiple( bfs )

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

  3. 深搜(DFS)广搜(BFS)详解

    图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...

  4. 【算法导论】图的广度优先搜索遍历(BFS)

    图的存储方法:邻接矩阵.邻接表 例如:有一个图如下所示(该图也作为程序的实例): 则上图用邻接矩阵可以表示为: 用邻接表可以表示如下: 邻接矩阵可以很容易的用二维数组表示,下面主要看看怎样构成邻接表: ...

  5. 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现

    1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...

  6. 【BZOJ5492】[HNOI2019]校园旅行(bfs)

    [HNOI2019]校园旅行(bfs) 题面 洛谷 题解 首先考虑暴力做法怎么做. 把所有可行的二元组全部丢进队列里,每次两个点分别向两侧拓展一个同色点,然后更新可行的情况. 这样子的复杂度是\(O( ...

  7. 深度优先搜索(DFS)和广度优先搜索(BFS)

    深度优先搜索(DFS) 广度优先搜索(BFS) 1.介绍 广度优先搜索(BFS)是图的另一种遍历方式,与DFS相对,是以广度优先进行搜索.简言之就是先访问图的顶点,然后广度优先访问其邻接点,然后再依次 ...

  8. 图的 储存 深度优先(DFS)广度优先(BFS)遍历

    图遍历的概念: 从图中某顶点出发访遍图中每个顶点,且每个顶点仅访问一次,此过程称为图的遍历(Traversing Graph).图的遍历算法是求解图的连通性问题.拓扑排序和求关键路径等算法的基础.图的 ...

  9. 数据结构与算法之PHP用邻接表、邻接矩阵实现图的广度优先遍历(BFS)

    一.基本思想 1)从图中的某个顶点V出发访问并记录: 2)依次访问V的所有邻接顶点: 3)分别从这些邻接点出发,依次访问它们的未被访问过的邻接点,直到图中所有已被访问过的顶点的邻接点都被访问到. 4) ...

随机推荐

  1. 第1章 PHP快速入门

    1.最好使用<?php   ?> 即XML风格的PHP标记: 2.echo语句:将传递给其自身的字符串打印到浏览器: 3.注释:①C风格的多行注释/*  */ ②C++风格的单行注释// ...

  2. cocos2dx 举例说明 convertToNodeSpace 与 convertToWorldSpace 的使用

    convertToNodeSpace:把世界坐标转换到当前节点的本地坐标系中. //可以应用于判断子节点是否被点击,这时就需要把坐标从世界坐标系转换为父节点的坐标系. //当然大多数情况会用CCMen ...

  3. hdu1536Nim

    sg函数打表的基础应用,第一道ac的sg函数打表题纪念下,直接上代码: hdu1536题目连接 #include<iostream> #include<cstdio> #inc ...

  4. Eclipse中安装Jdk和配置Python

    要借助Eclipse辅助工作,之前安装配置都是同事帮忙弄的,今天有空来整理一下安装配置步骤 一.安装JDK1.下载JDK,安装JDK,安装完毕,配置JDK环境变量  1)我的电脑右键-属性-高级-点击 ...

  5. HTML中判断手机是否安装某APP,跳转或下载该应用

    有些时候在做前端输出的时候,需要和app的做些对接工作.就是在手机浏览器中下载某app时,能判断该用户是否安装了该应用.如果安装了该应用,就直接打开该应用:如果没有安装该应用,就下载该应用.那么下面就 ...

  6. Hive 行列转换

    一.列转行 (对某列拆分,一列拆多行) 使用函数:lateral view explode(split(column, ',')) num eg: 如表:t_row_to_column_tmp 数据如 ...

  7. 关于ajax跨域问题

    什么是跨域 1.document.domain+iframe的设置 2.动态创建script 3.利用iframe和location.hash 4.window.name实现的跨域数据传输 5.使用H ...

  8. javaMail邮件发送的简单实现

    package com.test.mail; import java.util.Properties; import javax.mail.Message; import javax.mail.Ses ...

  9. python学习之批量更改文件格式

    文件操作 import os, glob from PIL import Image path = 'D:/SBSR/view_1' imgslist = glob.glob(path+'/*.jpg ...

  10. Linux文件挂载命令mount

    在linux系统中硬盘.u盘.光驱等其他设备都需要挂载后才能正常使用.下面是对挂载命令mount使用方法的一些总结. 文件挂载命令mountmount [-t 文件系统类型][-L卷标名][-o特殊选 ...