Find The Multiple
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,编写一个程序,找出n的非零倍数m,其十进制表示仅包含数字0和1。您可以假设n不大于200,并且有一个对应的m,包含不超过100个十进制数字。
输入
输入文件可能包含多个测试用例。每行包含一个n值(1<=n<=200)。包含零的行终止输入。
输出
对于输入中的每一个n值,打印一行包含对应的m值。m的十进制表示不能包含超过100位数字。如果给定值n有多个解,其中任何一个都可以接受。
思路
也就是求一个能把给出的数用0 1组成的数整除,输出任意一个这样的数。当i为奇数时模2余1,为偶数时模2余0。也是用递归的思想。下面两个代码,暴力的第二个就是把所有的0 1组成的数存起来,然后for循环索引。建议看一下中国剩余定理。
代码
#include<stdio.h>
#include<string.h>
int p,flag;
void dfs(int x,long long int y)
{
if(flag||x>19)//flag是用来保证当找到这个数时可以,终止这条路,
{
return ;
}
if(y%p==0)
{
flag=1;
printf("%lld\n",y);
return ;
}
dfs(x+1,y*10);
dfs(x+1,y*10+1);
return;
}
int main()
{
while(~scanf("%d",&p)&&p!=0)
{
flag=0;
dfs(1,1);
}
}
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
long long int a[1000000],s[1000000];
void dfs()
{
int m=1;
s[m]=1;
for(int i=2;i<=1000000;i=i+2)
{
s[i]=s[m]*10;
s[i+1]=s[m]*10+1;
m++;
}
}
int main()
{
int n,i;
dfs();
while(~scanf("%d",&n)&&n!=0)
{
for(i=1;i<1000000;i++)
{
if(s[i]%n==0)
{
printf("%lld\n",s[i]);
break;
}
}
}
return 0;
}