您的位置:首页 > 其它

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

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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: