Find The Multiple

给定正整数n,寻找一个只包含0和1的非零倍数m,保证m的长度不超过100位。对于每个测试用例,输出符合条件的m值,允许有多个解。思路涉及模2运算和递归思想,可以使用暴力枚举或中国剩余定理解决。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值