zoukankan      html  css  js  c++  java
  • POJ 1426 Find The Multiple

    Find The Multiple
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 12564   Accepted: 5175   Special Judge

    Description

    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
    

    BFS就可以解决。但是用自己编的队列94ms,用STL居然超时。
    STL代码(TLE):
    #include<iostream>
    #include<cstdio>
    #include<queue>
    using namespace std;
    typedef long long LL;
    LL m,n,ans;
    
    queue<LL>Q;
    void BFS()
    {
        Q.push(1);
        while(!Q.empty())
        {
            m=Q.front();
            Q.pop();
            for(int i=0;i<2;++i)
            {
                if(i==0)
                {
                    m*=10;
                }
                else
                {
                    m+=1;
                }
                if(m%n==0)
                {
                    ans=m;
                    return ;
                }
                else
                {
                    Q.push(m);
                }
            }
        }
    }
    int main()
    {
        while(scanf("%lld",&n)&&n)
        {
            while(!Q.empty())
            {
                        Q.pop();
            }
            BFS();
            printf("%lld\n",ans);
        }
    }

    自己编的队列实现(94ms):

    #include<iostream>
    #include<cstdio>
    using namespace std;
    typedef long long LL;
    LL m,n,ans;
    LL Q[1000000];
    LL Size=1000000-1;
    
    
    void BFS()
    {
        int rear=1;
        int front=1;
        Q[rear++]=1;
        while(true)
        {
            m=Q[front];
            front++;
            for(int i=0;i<2;++i)
            {
                if(i==0) m*=10;
                else     m+=1;
                if(m%n==0)
                {
                    ans=m;
                    return ;
                }
                else
                {
                    Q[rear++]=m;
                }
            }
        }
    }
    int main()
    {
        while(scanf("%lld",&n)&&n)
        {
            BFS();
            printf("%lld\n",ans);
        }
    }


     

  • 相关阅读:
    [MacOS]Sublime text3 安装(一)
    [RHEL8]开启BBR
    PAT Advanced 1136 A Delayed Palindrome (20分)
    PAT Advanced 1144 The Missing Number (20分)
    PAT Advanced 1041 Be Unique (20分)
    PAT Advanced 1025 PAT Ranking (25分)
    PAT Advanced 1022 Digital Library (30分)
    PAT Advanced 1019 General Palindromic Number (20分)
    PAT Advanced 1011 World Cup Betting (20分)
    PAT Advanced 1102 Invert a Binary Tree (25分)
  • 原文地址:https://www.cnblogs.com/liuyalunuli/p/2702144.html
Copyright © 2011-2022 走看看