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);
        }
    }


     

  • 相关阅读:
    Linux2.6内核实现的是NPTL
    linux kernel thread(Daemons)
    linux 多线程 LinuxThreads(转)
    同步与互斥的区别和联系
    Linux下使用popen()执行shell命令
    HTML学习笔记
    C++ 编译器的函数名修饰规则
    普林斯顿公开课 算法1-2:观察
    问卷星调查学生对《算法》教学的建议与反馈
    Visual Studio 2013 与 14
  • 原文地址:https://www.cnblogs.com/liuyalunuli/p/2702144.html
Copyright © 2011-2022 走看看