Find The Multiple

来源:互联网 发布:数据冰雹重点场所监控 编辑:程序博客网 时间:2024/06/16 16:12
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
26190
Sample Output
10100100100100100100111111111111111111
深度优先搜索到底是什么?说实话这道题能带给我很大的启发,深搜这两个字需要再好好斟酌斟酌。这里边有
几个好的小技巧值得我们去注意:深搜结束的条件是什么?一:找到了一个数,那么就不需要再找下去了。二:
这个数不能太大,或者说是超过无符号64位整数,也就是19位。另外深搜的变量变换值得研究。
#include<stdio.h>int flag;void DFS(int n,int k,unsigned __int64 x);int main(){int n;while(~scanf("%d",&n) && n){flag=0;DFS(n,0,1);}return 0; }   void DFS(int n,int k,unsigned __int64 x) { if(flag==1) return ;  if(k>=19) return ;  if(x%n==0) { printf("%I64u\n",x); flag=1; return ; }  DFS(n,k+1,x*10); DFS(n,k+1,x*10+1); }


0 0
原创粉丝点击