E - Find The Multiple(vj)

分两种情况搜索(ans*10,ans*10+1),搜索深度不大(k<20),满足条件及时退出。


#include<bits/stdc++.h>
using namespace std;
int n;
bool vis;
void dfs(long long ans,int k)
{
    if(vis) return ;
    if(k==19) return ;
    if(ans%n==0)
    {
        printf("%lld\n",ans);
        vis=true;
        return ;
    }
    dfs(ans*10,k+1);
    dfs(ans*10+1,k+1);
}
int main()
{
    while(scanf("%d",&n)==1&&n)
    {
        vis=false;
        dfs(1,0);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41061455/article/details/80344348
今日推荐