POJ 1426 Find The Multiple DFS
2017-05-20 17:07
423 查看
(欢迎阅读我的博客,如发现错误或有建议请评论留言,谢谢。)
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
Sample Output
找到一个只包含0,1的十进制数字,为n的倍数
题目输出不超100位,用无符号的longlong;
每一次搜索以两条路径 *10,*10+1;
因为输出的数字是不超过一百位的所以搜索不会超过10次;因为是两条路径,所以循环不超过20次,当第二十次的时候就结束;
代码如下:
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
找到一个只包含0,1的十进制数字,为n的倍数
题目输出不超100位,用无符号的longlong;
每一次搜索以两条路径 *10,*10+1;
因为输出的数字是不超过一百位的所以搜索不会超过10次;因为是两条路径,所以循环不超过20次,当第二十次的时候就结束;
代码如下:
#include<iostream> using namespace std; bool ok; int n; void dfs(unsigned long long x,int y,int z) { if(ok)return; if(x%n==0) { cout<<x<<endl; ok=1; return; } if(z==19)return; dfs(x*10,y,z+1); dfs(x*10+1,y,z+1); } int main() { while(cin>>n) { if(n==0)break; ok=0; dfs(1,n,0); } }
相关文章推荐
- POJ 1426 Find The Multiple (dfs)
- POJ-1426-Find The Multiple(BFS DFS)
- POJ1426——DFS——Find The Multiple
- POJ 1426 Find The Multiple --- BFS || DFS
- POJ 1426 Find The Multiple(DFS构造)
- Find The Multiple (dfs)POJ - 1426
- POJ 1426 Find The Multiple(dfs)
- Poj 1426--Find The Multiple(bfs或dfs)
- poj 1426 Find The Multiple (bfs / dfs)
- poj 1426 Find The Multiple dfs
- POJ 1426 Find The Multiple(dfs)
- POJ - 1426 Find The Multiple (暴力DFS)
- POJ 1426 Find The Multiple (DFS + 构造)
- POJ1426 Find The Multiple (BFS | DFS)
- POJ-1426(Find The Multiple) dfs
- 文章标题 POJ 1426 :Find The Multiple (dfs)
- POJ 题目1426 Find The Multiple(DFS)
- poj 1426 Find The Multiple(dfs || 二进制枚举)
- POJ-1426-Find The Multiple (BFS +DFS)两种解法
- poj 1426 Find The Multiple(DFS或打表)