Find The Multiple(二叉树)

Problem 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.
SampleInput

2
6
19
0

SampleOutput

10
100100100100100100
111111111111111111

#include<stdio.h>
#include<string.h>

int mod[100010],ans[100010];
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        if(n==0)break;
        memset(mod,0,sizeof(mod));
        memset(ans,0,sizeof(ans));
        int i;
        mod[1]=1%n;
        for(i=2;;i++)
        {
            mod[i]=(mod[i/2]*10+i%2)%n;//模仿双向bfs
            if(mod[i]==0)break;
        }
        int j=0;
        while(i)
        {
            ans[j++]=i%2;//哈弗曼树编码
            i/=2;
        }
        for(i=j-1;i>=0;i--)
        {
            printf("%d",ans[i]);
        }
        printf("\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/xjx19991226/article/details/82389513