llllmz

导航

Find The MultipleC++

这题就是找N的倍数m,M要求是由1和0组成且非0。

可以用图来看,从1出发临边是1和0,然后广度遍历,第一个能能整除N的数输出就行。

#include<iostream>
#include<queue>
using namespace std;

int main(){
    int n=-1;
    while(cin >> n){
        if(n==0) break;
        long long m=1;
        queue<long long> q;
        q.push(m);
        while(!q.empty()) {
            long long x = q.front();
            q.pop();
            if (x % n == 0) {//找到了
                cout << x << '\n';
                break;
            }
            long long t1 = x * 10;
            q.push(t1);
            q.push(t1+1);
        }
    }
    return 0;
}

结果如下:

posted on 2024-02-03 16:35  神奇的萝卜丝  阅读(26)  评论(0)    收藏  举报