码迷,mamicode.com
首页 > 其他好文 > 详细

poj 1426 Find The Multiple

时间:2015-10-02 16:04:25      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:

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

题意:给出一个整数n,(1 <= n <= 200)。求出任意一个它的倍数m,要求m必须只由十进制的‘0‘或‘1‘组成。
     如果搜到m则输出,否则搜索m×10和m×10+1直到得出答案

技术分享
 1 #include <iostream>
 2 #include <stack>
 3 #include <queue>
 4 #include <cstdio>
 5 using namespace std;
 6 #define LL unsigned long long
 7 int n;
 8 bool flag;
 9 void dfs(LL x,int step)
10 {
11     if(flag||step==19)//搜索到或者到第19步时返回,因为第20层就超出了unsigned long long范围
12         return ;
13     if(x%n==0)
14     {  //发现输出答案,并标记
15         printf("%llu\n",x);
16         flag=true;
17         return ;
18     }
19     dfs(x*10,step+1);
20     dfs(x*10+1,step+1);
21     return ;
22 }
23 int main()
24 {
25     while(scanf("%d",&n),n)
26     {
27         flag=false; //标记是否找到题意之中的m
28         dfs(1,0); // 从1开始搜索n的倍数
29     }
30     return 0;
31 }
View Code

 

 

poj 1426 Find The Multiple

标签:

原文地址:http://www.cnblogs.com/cxbky/p/4852227.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!