您的位置:首页 > 其它

Poj 1426--Find The Multiple(bfs或dfs)

2014-11-14 00:59 369 查看
Find The Multiple

Time Limit: 1000MSMemory Limit: 10000K
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
2
6
19
0

Sample Output
10
100100100100100100
111111111111111111

题意:输入一个数n,然后输出m,其中m是n的倍数,且m中的每位必须是1或者0;

开始的时候读错题意了,这个地方可以思考一下,开始的时候我从1开始入栈,然后把1的10倍入栈,然后把1的10倍+1的数入栈,以后以次类推。

这样就可以把所有的m满足各位为0或1的数,存到栈里,每次入栈时,只要再满足此时的m是n的倍数,输出m,程序就可以结束了。

因为这个题说了,答案不唯一,只要输出满足要求的就可以了,输出满足条件的最小的那个m;

#include<iostream>
#include<queue>
using namespace std;
int n;
long long BFS()
{
    long long t;
    queue<long long>q;
    while(!q.empty())
        q.pop();
    q.push(1);
    while(1)
    {
        t = q.front();
        if(t%n==0)
            return t;
        q.pop();
        q.push(t*10);
        q.push(t*10+1);
    }
}
int main()
{
    while(cin>>n, n)
    {
        long long res = BFS();
         cout<<res<<endl;
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: