2021春季个人赛-2 补题

F.Find The Multiple

题意: 给定一个整数 n n n,找到一个只有 0 0 0 1 1 1组成的数 m m m,使得 m m m整除 n n n
分析: D F S DFS DFS,每次 ∗ 10 *10 10 ∗ 10 + 1 *10+1 10+1,这样保证搜索的所有数字都是由 0 0 0 1 1 1组成。

代码:

#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
typedef unsigned long long LL;
int n,flag;
void dfs(LL x,int m){
    
    
    if(m>=20||flag) return ;
    if(x%n==0){
    
    
        cout<<x<<endl;
        flag=1;
        return ;
    }
    dfs(x*10,m+1);
    dfs(x*10+1,m+1);
}
int main(){
    
    
    while(cin>>n){
    
    
        if(!n) break;
        flag=0;
        dfs(1,1);
    }
}

猜你喜欢

转载自blog.csdn.net/messywind/article/details/114479333