zoukankan      html  css  js  c++  java
  • poj1426(暴力dfs)

    题目链接:https://vjudge.net/problem/POJ-1426

    题意:给出n(1<=n<=200),求出全部由01组成的能整除n的正整数。

    思路:此题在unsigned long long以内就可以找到满足条件的数,因此限制递归深度为20,然后枚举每一位两种可能即可。

    AC代码:

    #include<cstdio>
    #include<algorithm>
    using namespace std;
    
    int n,cnt,flag,res,ans[105];
    
    void dfs(int pos,int pre){
        if(pos>20) return;
        if(!pre){
            flag=1;
            res=pos;
            return;
        }
        ans[pos]=0;
        dfs(pos+1,(pre*10+0)%n);
        if(flag) return;
        ans[pos]=1;
        dfs(pos+1,(pre*10+1)%n);
    }
    
    int main(){
        while(~scanf("%d",&n),n){
            flag=0;
            ans[1]=1;
            dfs(2,1%n);
            for(int i=1;i<res;++i)
                printf("%d",ans[i]);
            printf("
    ");
        }
        return 0;
    }
  • 相关阅读:
    模块 hashlib模块
    设计模式
    类中双下方法
    工作小结 常见定制类
    python collections模块
    启动脚本
    anaconda镜像
    理解python的可变参数
    使用spark
    python 异常处理
  • 原文地址:https://www.cnblogs.com/FrankChen831X/p/11707648.html
Copyright © 2011-2022 走看看