zoukankan      html  css  js  c++  java
  • 初学算法之筛选素数法

    筛选求素数

    理解:把从1开始的、某一范围内的正整数从小到大顺序排列, 1不是素数,首先把它筛掉。剩下的数中选择最小的数是素数,然后去掉它的倍数。依次类推,直到筛子为空时结束。

    做题时的一些技巧:

    1.设求1….n中的素数

    只需使1到sqrt(n)的倍数去掉即可。

    2.设数组时,因为判断是否为素数只需要用到0和1两个值,则定义数组为bool型

    bool (只占一位)nu[111];

    bool型可扩大数组范围。(int 32位)

    例题:

    To improve the organization of his farm, Farmer John labels each of his N (1 <= N <= 5,000) cows with a distinct serial number in the range 1..20,000. Unfortunately, he is unaware that the cows interpret some serial numbers as better than others. In particular, a cow whose serial number has the highest prime factor enjoys the highest social standing among all the other cows.

    (Recall that a prime number is just a number that has no divisors except for 1 and itself. The number 7 is prime while the number 6, being divisible by 2 and 3, is not).

    Given a set of N (1 <= N <= 5,000) serial numbers in the range 1..20,000, determine the one that has the largest prime factor.  

    Input

    Line 1: A single integer, N 

    Lines 2..N+1: The serial numbers to be tested, one per lineOutput* Line 1: The integer with the largest prime factor. If there are more than one, output the one that appears earliest in the input file.Sample Input

    4
    36
    38
    40
    42

    Sample Output

    38

    这题有两个坑。
    1.数值1在题中默认为素数。
    2.多样例。
    我的代码:
     1 #include<stdio.h>
     2 #include<math.h>
     3 #include<string.h>
     4 #define y 22222
     5 int nu[y];
     6 int u=0;
     7 int main()
     8 {
     9     int t,n,f,m=0;
    10     int a;
    11     while(~(scanf("%d",&n)))
    12     {memset(nu, 0, sizeof(nu));
    13         f=0,m=0,a=0,u=0;
    14         while(n--)
    15         {
    16             
    17             scanf("%d",&t);
    18             for(int i=2;i<=sqrt(t);i++)
    19             {
    20                 a=i;
    21                 if(nu[a]==0)
    22                     while(a<=t)
    23                     {
    24                         a+=i;
    25                         nu[a]=1;
    26                         
    27                     }
    28             }
    29             for(int i=t;i>1;i--)
    30             {
    31                 if(nu[i]==0&&t%i==0)
    32                 {
    33                     f=i;
    34                     
    35                     m=(f>u)?t:m;
    36                     u=(f>u)?f:u;
    37                     break;
    38                     
    39                 }
    40                 
    41                 
    42             }
    43             
    44         }
    45         if(m!=0)
    46             printf("%d
    ",m);
    47         else
    48             printf("1
    ");
    49     }
    50     return 0;
    51 }

    后来算算这个代码的时间复杂度,羞愧的无地自容。

    但还有好多没有掌握,明天又要学新知识,且存,日后修改。

    欢迎各位在评论提出自己的思路 :)

  • 相关阅读:
    python中字典排序,列表中的字典排序
    Python模块:operator简单介绍
    java 物理资源回收 finally与try
    Eclipse相对路径
    java Lambda
    java 匿名内部类
    java File类
    java单例类
    java 创建子类
    jvm运行时数据区域
  • 原文地址:https://www.cnblogs.com/zmin/p/6298149.html
Copyright © 2011-2022 走看看