Find The Multiple

来源:互联网 发布:淘宝订单自动关闭 编辑:程序博客网 时间:2024/05/18 00:33
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

首先说一下题意题目要我们找一个十进制的数,划重点,输出是10进制,只是它只包含0和1的十进制数,并且这个数是给的输入的倍数。
一看便知道是深搜,我们就是在从高位到低位去找,最高位只能是1,然后不断地在后面+0(*10),+1(*10+1),就是这样的思路 ,19位就能出现所有的结果,到不了200,位,直接用long long 就可以存储

#include <iostream>#include<cstdio>#include<cstring>using namespace std;typedef unsigned long  long ll;//usigned long long 是20位,long long 是19位int n;int flag;ll ans;//这个题感觉还是有点”蒙“的成分在里面,不知道为什么会这样,只是猜测会在20位的数据里面出结果void  dfs(ll now,int step){    if(step>19)//超过19位,就不要再搜了,也超出了ll的数据范围        return;    if(flag)        return;    if(now%n==0)    {        flag=1;        ans=now;        return ;    }    dfs(now*10,step+1);   //这种添加的方式还是很简单的    if(flag)   return;    dfs(now*10+1,step+1); //记住这种末尾加1还是加0的方式}int main(){    while(~scanf("%d",&n))    {       if(n==0) break;       flag=0;       dfs(1,1);//深搜不能超界       printf("%I64d\n",ans);    }    return 0;}
原创粉丝点击