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. POJ 3139 Balancing the Scale

    枚举. 看了这个方法:$http://www.cppblog.com/shiming413/archive/2008/12/21/29671.html$ 将数字归类的地方不能用$vector$,会超时 ...

  2. JavaScriptSerializer返回一条Json,页面获取值问题,数据绑定

    一,后台处理数据方法 JavaScriptSerializer serializer = new JavaScriptSerializer(); string js = serializer.Seri ...

  3. delphi倒计时按钮写法

    procedure TForm1.FormActivate(Sender: TObject); var i: Integer; begin btn8.Enabled:=False; do begin ...

  4. Scala文件操作

    Scala中的文件操作基本可以依赖于Java的实现,包括输入.输出流的使用. object FileOps {def main(args: Array[String]) { val file = So ...

  5. scala实现快速排序

    scala> def qSort(a: List[Int]): List[Int] = { | ) a | else qSort( a.filter(a.head > _ )) ++ | ...

  6. ajax jsonp 跨域请求

    $.ajax({ type:"get", url: "http://localhost/test/a.php", dataType: "jsonp&q ...

  7. 设置session存储在int sqlserver上---使用aspnet_regsql.exe工具

    以管理员身份打开命令窗 1)cd到相应的framework下,如:C:\Windows\Microsoft.NET\Framework\v4.0.30319 2)执行如下命令:aspnet_regsq ...

  8. 定时发布任务,在global.asax中获取文件的物理路径的方法

    如果要把一个相对路径或者虚拟路径映射道服务器的物理路径,通常会使用Server.MapPath()函数,比如将根目录下的html目录映射为物理路径:Server.MapPath("html& ...

  9. 学习自动化工具gulp

    <什么是gulp>官网地址:http://gulpjs.com/ gulp是可以自动化执行任务的工具,在开发流程里,一定有一些动作需要手工的重复的去执行,例如: ·把一个文件拷贝到另外一个 ...

  10. meta小解

    meta是html中的一个辅助标签,位于<head>与<title>之间,它能提供用户不可见的信息,数据结构为键值对 meta标签格式<meta http-equiv/n ...