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类型代码:

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<queue> using namespace std; queue<string>s;
int a,c;
string b; int chu(string s)
{
c=;
for(int i=;i<s.length();i++){
c*=;
c+=s[i]-'';
c%=a;
}
if(c==)
return ;
else
return ;
} int bfs()
{
while(!s.empty()){
b=s.front();
s.pop();
if(b.length()>)
continue;
if(chu(b+'')==){
cout<<b+''<<endl;
while(!s.empty()){
s.pop();
}
return ;
}
else{
s.push(b+'');
} if(chu(b+'')==){
cout<<b+''<<endl;
while(!s.empty()){
s.pop();
}
return ;
}
else{
s.push(b+'');
}
}
return ;
} int main()
{
// freopen("input.txt","r",stdin);
while(cin>>a){
if(a==)
break;
else{
s.push("");
bfs();
}
}
return ;
}

long long类型代码:

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<queue> using namespace std; queue<long long>s;
int a,c;
long long b; int bfs()
{
while(!s.empty()){
b=s.front();
s.pop();
if(b%a==){
cout<<b<<endl;
while(!s.empty()){
s.pop();
}
return ;
}
else{
s.push(b*);
s.push(b*+);
}
}
return ;
} int main()
{
// freopen("input.txt","r",stdin);
while(cin>>a){
if(a==)
break;
else{
s.push();
bfs();
}
}
return ;
}

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. js作用域详解

    // 作用域:(1)域:空间.范围.区域……     (2) 作用:读.写 script 全局变量.全局函数 自上而下 函数 由里到外 浏览器: “JS解析器” 1)“找一些东西” :var func ...

  2. Playmaker 基础使用与案例操作

    首先是把下载好的插件导入Unity工程中. ▼导入完成后第一个动作就是检查下拉菜单里面是否已经增加了Playmaker的功能,如果在安装后没看到Playmaker的菜单,一般情况下直接点击菜单上的空白 ...

  3. AnimatorController反向运动学IK

    通过使用反向运动学IK,我们可以根据需要控制角色身体某个特定部位进行旋转或移动,达到想要的一些效果,比如:在移动时,让一只脚带伤拖行:让手抬起去拿桌上的苹果:让脑袋一直面向我们的色像机,就像一直注视着 ...

  4. ContourLine

    #define MULTI_PLOT true //Determine whether or not to plot multiple iterations. #define X_MAX 1.0 // ...

  5. jquery 操作listbox 左右相互选择

    实现左右两个listbox的相互选择功能 代码如下: function ListBox_Move(listfrom, listto) { var size = $j("#" + l ...

  6. mysql随记

    .frm是描述了表的结构,.MYD保存了表的数据记录,*.MYI则是表的索引 ibd是MySQL数据文件.索引文件,无法直接读取. ibdata是innodb引擎使用的 如果是使用myisam引擎 则 ...

  7. spring mvc 实现文件上传下载

    /** * 文件上传 * @param pictureFile */ @RequestMapping("/reportupload") public ResponseInfo up ...

  8. $.ajax()方法详解 jquery中的ajax方法

    jquery中的ajax方法参数总是记不住,这里记录一下. 1.url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. 2.type: 要求为String类型的参数,请求方式(p ...

  9. 绝对路径&相对路径

    被绝对路径和相对路径搞疯了,好多地方不一样,从今天开始,记录下来每次关于这个问题的记录,以备查用 css文件里: 绝对路径:以"/"开头,表示从项目的根目录开始

  10. LYF电子书制作工具(CHM格式)

    可以制作CHM电子书 可以制作电子相册 下载地址:http://files.cnblogs.com/files/blogLYF/LYF电子书制作安装包.zip http://files.cnblogs ...