POJ 1426 Find The Multiple

来源:互联网 发布:骂交警被拘 知乎 编辑:程序博客网 时间:2024/04/29 19:18
Find The Multiple
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 13335 Accepted: 5445 Special Judge

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

26190

Sample Output

10100100100100100100111111111111111111

Source

Dhaka 2002
这题考的是bfs要注意控制数组的大小,否则很容易就re了,用bfs的时候不要每个结构体把整个结果表示出来,这样会超内存,应该设置一个指针,每个结构体中有一个字符,和一个指针,来指向谁产生的他,这样就ok了
#include <stdio.h>#include <string.h>struct num{    char c;    int pt;}queue[1100010];int Top;char s2[1000];int main(){    void bfs(int n);    int i,j,n,m,s,k;    while(scanf("%d",&n)!=EOF)    {        if(n==0)        {            break;        }        bfs(n);        for(i=Top;i>=0;i--)        {            printf("%c",s2[i]);        }        printf("\n");    }    return 0;}void bfs(int n){    int i,j,base,top,s,t;    base=top=0;    queue[top].pt=-1;    queue[top++].c='1';    while(base<top)    {        t=base;        for(i=0;;i++)        {            s2[i]=queue[t].c;            t=queue[t].pt;            if(t==-1)            {                break;            }        }        for(j=i,s=0;j>=0;j--)        {            s=s*10+s2[j]-'0';            s=s%n;        }        if(s==0)        {            Top=i;            break;        }        queue[top].c='0'; queue[top++].pt=base;        queue[top].c='1'; queue[top++].pt=base;        base++;    }}