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. 编译C语言单元测试框架CUnit库的方法

    引用: http://blog.csdn.net/yygydjkthh/article/details/46357421 个人备忘使用 /******************************* ...

  2. SQL2008将服务器的数据库表数据插入到本地数据库

    一,配置参数 exec sp_configure reconfigure exec sp_configure RECONFIGURE 若不配置参数会出现,提示这个错误: SQL Server 阻止了对 ...

  3. trove,测试,db小解析

    # Copyright 2014 Tesora Inc.# All Rights Reserved.## Licensed under the Apache License, Version 2.0 ...

  4. 顶层const和底层const

    As we’ve seen, a pointer is an object that can point to a different object. As a result,we can talk ...

  5. mysql随记

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

  6. 关于Java集合

    之前关于java集合认识,虽然理解,但是总是忘记关键点,今明两天写一篇关于集合的随笔

  7. Ecstore启用www.ecstore.com和m.ecstore.com域名

    Ecstore启用www.ecstore.com和m.ecstore.com域名 修改config/mapper.php if($_SERVER['SERVER_NAME']=='www.ecstor ...

  8. install webapp2 on Linux outside google app engine.

    Reference: https://webapp-improved.appspot.com/tutorials/quickstart.nogae.html Step 1: install pip S ...

  9. ASCII码对应表chr(num)

    chr(9) tab空格       chr(10) 换行      chr(13) 回车        Chr(13)&chr(10) 回车换行       chr(32) 空格符      ...

  10. 将可执行exe文件注册成windows服务

    要把应用程序添加为服务,你需要两个小软件:Instsrv.exe和Srvany.exe.Instsrv.exe可以给系统安装和删除服务,Srvany.exe可以让程序以服务的方式运行.这两个软件都包含 ...