zoukankan      html  css  js  c++  java
  • usaco-pprime-pass

    这个按自己的思路,超时,网上找到的资料,这个最简单,可是有2句很精妙:

        k=i;j=1;
            while(k>9){k/=10;j*=10;}
            if(k%2==0){i+=j;continue;}

    跳过首位为0,2,4,6,8的回文数。能剪断一半,提升一倍的效率。

    /*
    ID: qq104801
    LANG: C++
    TASK: pprime
    */
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <assert.h>
    #include <math.h>
    
    char x[11];
    int a,b;
    
    int isprime(int n)
    {    
        if (n%2==0)return 0;
        for(int i=3;i*i<=n;i+=2)
            if(n%i==0)return 0;    
        return 1;
    }
    
    int modify(int x)
    {
        int t=x;x/=10;
        while(x>0){
            t=t*10+x%10;
            x/=10;
        }
        return t;
    }
    
    void test()
    {    
        FILE *fin = fopen("pprime.in", "r");
        FILE *fout = fopen("pprime.out", "w"); 
        fscanf(fin,"%d %d",&a,&b);
        if(a<=5&&5<=b)fprintf(fout,"5
    ");
        if(a<=7&&7<=b)fprintf(fout,"7
    ");
        if(a<=11&&11<=b)fprintf(fout,"11
    ");
        int i=10,j,k;
        while(modify(i)<a)i++;
        while(modify(i)<=b)
        {
            k=i;j=1;
            while(k>9){k/=10;j*=10;}
            if(k%2==0){i+=j;continue;}
            j=modify(i);
            if(isprime(j))
                fprintf(fout,"%d
    ",j);
            i++;
        }   
        
        fclose(fin);
        fclose(fout);
    }
    
    main () {    
        //printf("%d
    ",ispalindromes(11));
        test();    
        //printf("%d
    ",modify(32));
        exit (0);
    }

    测试结果:

    USER: cn tom [qq104801]
    TASK: pprime
    LANG: C++
    
    Compiling...
    Compile: OK
    
    Executing...
       Test 1: TEST OK [0.005 secs, 3496 KB]
       Test 2: TEST OK [0.003 secs, 3496 KB]
       Test 3: TEST OK [0.008 secs, 3496 KB]
       Test 4: TEST OK [0.011 secs, 3496 KB]
       Test 5: TEST OK [0.011 secs, 3496 KB]
       Test 6: TEST OK [0.008 secs, 3496 KB]
       Test 7: TEST OK [0.024 secs, 3496 KB]
       Test 8: TEST OK [0.022 secs, 3496 KB]
       Test 9: TEST OK [0.030 secs, 3496 KB]
    
    All tests OK.
    
    Your program ('pprime') produced all correct answers! This is your submission #3 for this problem. Congratulations!
    
    Here are the test data inputs:
    
    ------- test 1 ----
    5 500
    ------- test 2 ----
    750 14000
    ------- test 3 ----
    123456 1123456
    ------- test 4 ----
    97000 1299000
    ------- test 5 ----
    9878210 9978210
    ------- test 6 ----
    9902099 9902100
    ------- test 7 ----
    7 10000000
    ------- test 8 ----
    1333331 9743479
    ------- test 9 ----
    5 100000000
    
    Keep up the good work!
    Thanks for your submission!
    /***********************************************

    看书看原版,原汁原味。

    不会英文?没关系,硬着头皮看下去慢慢熟练,才会有真正收获。

    没有原书,也要网上找PDF来看。

    网上的原版资料多了去了,下载东西也到原始下载点去看看。

    你会知其所以然,呵呵。

    ***********************************************/

  • 相关阅读:
    pycharm中启动Django方法
    Python ——selenium报错 'chromedriver.exe' executable needs to be in PATH
    软件测试
    C#&.Net干货分享- 构建PrinterHelper直接调用打印机相关操作
    C#&.Net干货分享- iTextSharp导出数据源到PDF
    C#&.Net干货分享-构建Aocr_ImageHelper读取图片文字做解析
    C#&.Net干货分享-构建后台自动定时任务的源码
    SQL Server清理数据库日志的脚本-干货
    SQL Server通过函数把逗号分隔的字符串拆分成数据列表的脚本-干货
    SQL Server通过定义函数返回字段数据列表模板-干货
  • 原文地址:https://www.cnblogs.com/dpblue/p/3950595.html
Copyright © 2011-2022 走看看