zoukankan      html  css  js  c++  java
  • Find The Multiple 《DFS》

    Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains only the digits 0 and 1. You may assume that n is not greater than 200 and there is a corresponding m containing no more than 100 decimal digits.

    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

    For each value of n in the input print a line containing the corresponding value of m. The decimal representation of m must not contain more than 100 digits. If there are multiple solutions for a given value of n, any one of them is acceptable.

    Sample Input

    2
    6
    19
    0

    Sample Output

    10
    100100100100100100
    111111111111111111


    最初想用队列搞一下,就像抓住那头牛一样,结果没搞过去,然后,DFS跑一遍能A掉。
    可以的话可以用二进制码搞一下,感觉应该能过


    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <string>
    #include <cstring>
    #include <cstdlib>
    #include <map>
    #include <vector>
    #include <set>
    #include <queue>
    #include <stack>
    #include <cmath>
    using namespace std;
    #define ull unsigned long long
    #define lli long long
    #define pq priority_queue<int>
    #define pql priority_queue<ll>
    #define pqn priority_queue<node>
    #define v vector<int>
    #define vl vector<ll>
    #define read(x) scanf("%d",&x)
    #define lread(x) scanf("%lld",&x);
    #define pt(x) printf("%d
    ",(x))
    #define YES printf("YES
    ");
    #define NO printf("NO
    ");
    #define gcd __gcd
    #define out(x) cout<<x<<endl;
    #define over cout<<endl;
    #define rep(j,k) for (int i = (int)(j); i <= (int)(k); i++)
    #define input(k) for (int i = 1; i <= (int)(k); i++)  {scanf("%d",&a[i]) ; }
    #define mem(s,t) memset(s,t,sizeof(s))
    #define ok return 0;
    #define TLE std::ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    #define mod(x) ((x)%9973)
    #define test cout<<"     ++++++      "<<endl;
    //二叉树
    #define lson rt<<1, l, m
    #define rson rt<<1|1, m+1, r
    //线段树
    #define ls now<<1
    #define rs now<<1|1
    const int MAXN = 2e5+5;
    //int dir[6][3] = {0,0,1,0,0,-1,1,0,0,-1,0,0,0,1,0,0,-1,0};
    //int dir[4][2] = {1,0,-1,0,0,1,0,-1}; //单位移动
    //int dir[8][2] = {2,1,2,-1,-2,1,-2,-1,1,2,1,-2,-1,2,-1,-2};
    int t,n,m,x,y,col,ex,ey,ans,ly,flag;
    struct node
    {
        int id;
        int l,r;
    } dp[7000+5];
    int cmp(node a,node b)
    {
        return a.l<b.l;
    }
    int cmd(node a,node b)
    {
        return a.r>b.r;
    }
    void DFS(ull k,int cnt)
    {
        if(flag==1) return ;
        if(k%n==0)
        {
    
            cout<<k<<endl;
            flag = 1;
            return ;
        }
        if(cnt==19) return ;
        DFS( k*10 ,cnt+1);
        DFS( k*10+1 ,cnt+1);
    }
    int main()
    {
        TLE;
        while(cin>>n&&n)      //这里的&&n 要有,不然的话会RE,原因是除0了
        {
            flag=0;
            DFS(1,0);
        }
        ok;
    }
    
    
    /*
    int main()
    {
        while(cin>>n)
        {
            ans=0;
            for(int i=0; i<n; i++)
                cin>>dp[i].l;
            for(int i=0; i<n; i++)
                cin>>dp[i].r;
            sort(dp,dp+n,cmp);
    
            for(int i=0; i<n; i++)
                cout<<dp[i].l;
    
            for(int i=0; i<n; i++)
            {
                if(dp[i].l==dp[i+1].l || dp[i].l==dp[i-1].l)
                {
                    ans += dp[i].r;
                    dp[i].r=0;
                }
            }
            sort(dp,dp+n,cmd);
            cout<<ans+dp[0].r<<endl;
        }
    }
    */


    所遇皆星河
  • 相关阅读:
    Java学习第一周汇报
    Java暑期学习第八天日报
    Java暑期学习第十天日报
    Java暑期学习第十二天日报
    Java学习第二周汇报
    Java暑期学习第九天日报
    0006 列表(ul、ol、dl)
    0015 行高那些事:lineheight
    0016 CSS 背景:background
    HTTP中GET与POST的区别 99%的错误认识
  • 原文地址:https://www.cnblogs.com/Shallow-dream/p/11585960.html
Copyright © 2011-2022 走看看