POJ1426Find The Multiple

来源:互联网 发布:手机c语言编程 编辑:程序博客网 时间:2024/06/05 14:29

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


题目大意:求n的倍数,但倍数的十进制数都是由0和1构成且不超过一百位。

方法:n小于200,bfs和dfs都可以,但是dfs注意要控制下边界。看了比人的博客,感觉有点高深,我还是这么水过去了吧。


dfs:

#include<iostream>#include<climits>#include<cstring>using namespace std;unsigned long long a;bool found;void dfs(unsigned long long tem, int level){    if(found || level >18)        return ;    if(tem % a == 0)    {        found = true;        cout << tem <<endl;        return;    }    dfs(tem*10, level+1);    if(found)        return ;    dfs(tem*10+1, level+1);}int main(){    while(cin >> a && a)    {        found = false;        dfs(1, 0);    }    return 0;}


bfs:

#include<iostream>#include<climits>#include<queue>using namespace std;long long bfs(long long a){    queue<long long> q;    q.push(1);    while(!q.empty())    {        long long tem = q.front();        q.pop();        if(tem % a == 0)            return tem;        q.push(tem*10);        q.push(tem*10 + 1);    }    return 10000000;}int main(){    long long b, ans[201];    for(long long int i = 1; i <= 200; i++)        ans[i] = bfs(i);    while(cin >> b && b)    {        cout << ans[b] << endl;    }    return 0;}



0 0