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 (0) 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

个人心得:这题开始没看懂,原来是求所求数的倍数,但这个倍数只由0,1组成,有俩种版本,一个是longlong直接过,这个在数据较多的时候不容易过,

先说下longlong吧,就是从1开始深搜,有2种方向,一是乘以10,二是乘以10再加1,这个简单深搜还是比较好实现的。

  1. #include<iostream>
  2. #include<cstring>
  3. #include<cstdio>
  4. #include<cmath>
  5. #include<queue>
  6. #include<algorithm>
  7. using namespace std;
  8. int n,flag;
  9. unsigned long long ans;
  10. void dfs(unsigned long long x,int y)
  11. {
  12. if(y>) return ;
  13. if(x%n==)
  14. {
  15. flag=;
  16. ans=x;
  17. return;
  18.  
  19. }
  20. else
  21. {for(int i=;i<;i++)
  22. dfs(*x+i,y+);
  23. }
  24. if(flag) return;
  25.  
  26. }
  27. int main()
  28. {
  29. unsigned long long x;
  30. while(cin>>n)
  31. {
  32. if(n==) break;
  33. x=;
  34. flag=;
  35. dfs(x,);
  36. cout<<ans<<endl;
  37.  
  38. }
  39. return ;
  40.  
  41. }

再看一下数组存储的代码,他这个涉及到的知识点真的不知道,每次得到新数都进行取余再将这个数放入数组里,这样就不会数据溢出了,一开始想这么做怕担心

数据会溢出。

  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int n, t, a[], ac,l;
  6.  
  7. void dfs(int c, int s)
  8.  
  9. {
  10.  
  11. if(s==)//如果余数为0,择停止递归
  12.  
  13. {
  14.  
  15. ac = ;//表示已经找到结果
  16.  
  17. l=c;//给数组的长度赋值
  18.  
  19. }
  20.  
  21. else if(c < )//在深度100范围内进行递归
  22.  
  23. {
  24.  
  25. if(ac==)//还没找到结果,进行递归运算
  26.  
  27. {
  28.  
  29. a[c] = ;//尝试下一位放1
  30.  
  31. dfs(c + , (s * + ) % n);
  32.  
  33. }
  34.  
  35. if(ac==)
  36.  
  37. {
  38.  
  39. a[c] = ;
  40.  
  41. dfs(c + , (s * ) % n);
  42.  
  43. }
  44.  
  45. }
  46.  
  47. }
  48.  
  49. int main()
  50.  
  51. {
  52.  
  53. while(cin>>n,n)
  54.  
  55. {
  56.  
  57. a[] = , ac = ;//初始化
  58.  
  59. dfs(, );
  60.  
  61. for(int i=;i<l;i++) cout<<a[i];//根据递归得到的结果,进行输出
  62.  
  63. cout<<endl;
  64.  
  65. }
  66.  
  67. return ;
  68.  
  69. }

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

  1. 【POJ - 1426】Find The Multiple(dfs)

    -->Find The Multiple 原文是英语,直接上中文了 Descriptions: 给定一个正整数n,请编写一个程序来寻找n的一个非零的倍数m,这个m应当在十进制表示时每一位上只包含 ...

  2. LeetCode Subsets II (DFS)

    题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) class Solution { publ ...

  3. LeetCode Subsets (DFS)

    题意: 给一个集合,有n个互不相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: DFS方法:由于集合中的元素是不可能出现相同的,所以不用解决相同的元素而导致重复统计. class Sol ...

  4. HDU 2553 N皇后问题(dfs)

    N皇后问题 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description 在 ...

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

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

  6. 【算法导论】图的深度优先搜索遍历(DFS)

    关于图的存储在上一篇文章中已经讲述,在这里不在赘述.下面我们介绍图的深度优先搜索遍历(DFS). 深度优先搜索遍历实在访问了顶点vi后,访问vi的一个邻接点vj:访问vj之后,又访问vj的一个邻接点, ...

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

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

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

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

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

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

随机推荐

  1. iOS 52个技巧学习心得笔记 第二章 对象 , 消息, 运行期

    1. 属性 在开发过程中经常要用到定义属性,@property和@synthesize是经常用到的属性, property在.h文件中作声明,@synthesize在.m文件中用于实现 // Stud ...

  2. UI控件之UICollectionView

    UICollectionView:集合视图,是iOS6.0后出现的,与UITableView类似,优势在于可以灵活的布局cell UICollectionViewLayout:布局类,抽象类,一般定义 ...

  3. 【leetcode刷题笔记】Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  4. CSS3展开带弹性动画的手风琴菜单

    在线演示 本地下载

  5. Go struct tag

    struct成员变量标签(Tag)说明 要比较详细的了解这个,要先了解一下golang的基础,在golang中,命名都是推荐都是用驼峰方式,并且在首字母大小写有特殊的语法含义:包外无法引用.但是由经常 ...

  6. Linux VPS实用简单安全配置

    今天,和大家一起来分享VPS最基本的安全配置. 第一.修改SSH端口 VPS默认的SSH端口是22,那些扫描穷举密码的,也势必从22开始,所以,修改22为一个其他的数字,是非常有必要的. 好了,SSH ...

  7. Spring的AOP面向切面编程

    什么是AOP? 1.AOP概念介绍 所谓AOP,即Aspect orientied program,就是面向方面(切面)的编程. 功能: 让关注点代码与业务代码分离! 关注点: 重复代码就叫做关注点: ...

  8. python进阶01

    基础部分python学完,咱们可以深入了解下python高阶知识点,让我们一起来学习吧! 1.面向对象进阶 1)元类 1.1.1:python创建类原理 python创建类: class Object ...

  9. MySQL操作的相关命令

    拷贝表,并且复制两条数据到新表中 create table t_comments_sample2 like t_comments_sample; #拷贝表结构 ,;#复制两条数据 MySQL Work ...

  10. java.sql.SQLException: Incorrect string value: '\xF0\x9F\x91\x88\xE6\x88...' for column 'content' at row 1

    往MySQL插入数据时,报错如下 java.sql.SQLException: Incorrect at com.mysql.cj.jdbc.exceptions.SQLError.createSQL ...