Find The Multiple 题解

 
The long-lost Sunday is coming again, and the ACM Laboratory Elimination Competition of Beihua University has begun. Now you have to complete a task: XLS is very happy, because IG won. Now to make him happy, just ask you to help him solve this problem, give you an n, let you find a multiple of M and m for n, but M has a limit. His number is only 0 or 1\. Can you help him solve this problem?

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

Each input you export a corresponding M, m does not know the unique number of m does not exceed 200 bits.

Sample Input

100
2

Sample Output

100
10

 

 题意理解:

  老实说,我刚开始看了很久都没看懂==。其实题目的意思呐就是在1和0组成的数里面找n的倍数,这样是不是清晰多了,害

DFS搜索法:
#include<stdio.h>
#include<iostream>
#include<queue>
#include<math.h>
using namespace std;

long long n,now,cnt;

long long bfs(long long n)
{
    queue<long long> q;
    q.push(1);
    while(q.size())
    {
        now=q.front();
        q.pop();
        cnt=(long long)log10(now)+1;
//        printf("yi--%lld %lld\n",cnt,now);
        if(cnt>200) break;
        if(now%n==0) return now;
        q.push(now*10);
        q.push(now*10+1);//若有其一除得尽,则return ; 
    }
    return 0;
}

int main()
{
    while(cin>>n && n)
    {
        cnt=0;
        cout<<bfs(n)<<endl;
    }
    return 0;
}

 


posted @ 2020-01-22 14:38  Ilaria  阅读(126)  评论(0编辑  收藏  举报