Find The Multiple(DFS)
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,这个简单深搜还是比较好实现的。
- #include<iostream>
- #include<cstring>
- #include<cstdio>
- #include<cmath>
- #include<queue>
- #include<algorithm>
- using namespace std;
- int n,flag;
- unsigned long long ans;
- void dfs(unsigned long long x,int y)
- {
- if(y>) return ;
- if(x%n==)
- {
- flag=;
- ans=x;
- return;
- }
- else
- {for(int i=;i<;i++)
- dfs(*x+i,y+);
- }
- if(flag) return;
- }
- int main()
- {
- unsigned long long x;
- while(cin>>n)
- {
- if(n==) break;
- x=;
- flag=;
- dfs(x,);
- cout<<ans<<endl;
- }
- return ;
- }
再看一下数组存储的代码,他这个涉及到的知识点真的不知道,每次得到新数都进行取余再将这个数放入数组里,这样就不会数据溢出了,一开始想这么做怕担心
数据会溢出。
- #include <iostream>
- using namespace std;
- int n, t, a[], ac,l;
- void dfs(int c, int s)
- {
- if(s==)//如果余数为0,择停止递归
- {
- ac = ;//表示已经找到结果
- l=c;//给数组的长度赋值
- }
- else if(c < )//在深度100范围内进行递归
- {
- if(ac==)//还没找到结果,进行递归运算
- {
- a[c] = ;//尝试下一位放1
- dfs(c + , (s * + ) % n);
- }
- if(ac==)
- {
- a[c] = ;
- dfs(c + , (s * ) % n);
- }
- }
- }
- int main()
- {
- while(cin>>n,n)
- {
- a[] = , ac = ;//初始化
- dfs(, );
- for(int i=;i<l;i++) cout<<a[i];//根据递归得到的结果,进行输出
- cout<<endl;
- }
- return ;
- }
Find The Multiple(DFS)的更多相关文章
- 【POJ - 1426】Find The Multiple(dfs)
-->Find The Multiple 原文是英语,直接上中文了 Descriptions: 给定一个正整数n,请编写一个程序来寻找n的一个非零的倍数m,这个m应当在十进制表示时每一位上只包含 ...
- LeetCode Subsets II (DFS)
题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) class Solution { publ ...
- LeetCode Subsets (DFS)
题意: 给一个集合,有n个互不相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: DFS方法:由于集合中的元素是不可能出现相同的,所以不用解决相同的元素而导致重复统计. class Sol ...
- HDU 2553 N皇后问题(dfs)
N皇后问题 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description 在 ...
- 深搜(DFS)广搜(BFS)详解
图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...
- 【算法导论】图的深度优先搜索遍历(DFS)
关于图的存储在上一篇文章中已经讲述,在这里不在赘述.下面我们介绍图的深度优先搜索遍历(DFS). 深度优先搜索遍历实在访问了顶点vi后,访问vi的一个邻接点vj:访问vj之后,又访问vj的一个邻接点, ...
- 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现
1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...
- 深度优先搜索(DFS)和广度优先搜索(BFS)
深度优先搜索(DFS) 广度优先搜索(BFS) 1.介绍 广度优先搜索(BFS)是图的另一种遍历方式,与DFS相对,是以广度优先进行搜索.简言之就是先访问图的顶点,然后广度优先访问其邻接点,然后再依次 ...
- 图的 储存 深度优先(DFS)广度优先(BFS)遍历
图遍历的概念: 从图中某顶点出发访遍图中每个顶点,且每个顶点仅访问一次,此过程称为图的遍历(Traversing Graph).图的遍历算法是求解图的连通性问题.拓扑排序和求关键路径等算法的基础.图的 ...
随机推荐
- iOS 52个技巧学习心得笔记 第二章 对象 , 消息, 运行期
1. 属性 在开发过程中经常要用到定义属性,@property和@synthesize是经常用到的属性, property在.h文件中作声明,@synthesize在.m文件中用于实现 // Stud ...
- UI控件之UICollectionView
UICollectionView:集合视图,是iOS6.0后出现的,与UITableView类似,优势在于可以灵活的布局cell UICollectionViewLayout:布局类,抽象类,一般定义 ...
- 【leetcode刷题笔记】Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- CSS3展开带弹性动画的手风琴菜单
在线演示 本地下载
- Go struct tag
struct成员变量标签(Tag)说明 要比较详细的了解这个,要先了解一下golang的基础,在golang中,命名都是推荐都是用驼峰方式,并且在首字母大小写有特殊的语法含义:包外无法引用.但是由经常 ...
- Linux VPS实用简单安全配置
今天,和大家一起来分享VPS最基本的安全配置. 第一.修改SSH端口 VPS默认的SSH端口是22,那些扫描穷举密码的,也势必从22开始,所以,修改22为一个其他的数字,是非常有必要的. 好了,SSH ...
- Spring的AOP面向切面编程
什么是AOP? 1.AOP概念介绍 所谓AOP,即Aspect orientied program,就是面向方面(切面)的编程. 功能: 让关注点代码与业务代码分离! 关注点: 重复代码就叫做关注点: ...
- python进阶01
基础部分python学完,咱们可以深入了解下python高阶知识点,让我们一起来学习吧! 1.面向对象进阶 1)元类 1.1.1:python创建类原理 python创建类: class Object ...
- MySQL操作的相关命令
拷贝表,并且复制两条数据到新表中 create table t_comments_sample2 like t_comments_sample; #拷贝表结构 ,;#复制两条数据 MySQL Work ...
- 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 ...