杭电POJ-2161-Primes

来源:互联网 发布:java将图片生成二维码 编辑:程序博客网 时间:2024/06/03 21:34

题目描述:

Problem DescriptionWrite a program to read in a list of integers and determine whether or not each number is prime. A number, n, is prime if its only divisors are 1 and n. For this problem, the numbers 1 and 2 are not considered primes.  InputEach input line contains a single integer. The list of integers is terminated with a number<= 0. You may assume that the input contains at most 250 numbers and each number is less than or equal to 16000. OutputThe output should consists of one line for every number, where each line first lists the problem number, followed by a colon and space, followed by "yes" or "no".  Sample Input12345170 Sample Output1: no2: no3: yes4: no5: yes6: yes
个人理解:

需要素数打表 另外注意是<=0才结束 其次就是 2在这道题目中不是素数

运行结果:

ResultMemoryTimeLanguageCode lengthAccepted1840k0MSC373BAC代码:

# include <stdio.h># include <math.h>char A[16001]={"\0"};int main(){    int m=0,n=1,i,j,Q=sqrt(M);    for(i=2;i<=Q;i++)//改进的埃氏筛法O(sqrt(N)*log(long(N)))       if(!A[i])for(j=i+i;j<M;j+=i)             A[j]=1;          A[1]=A[2]=1;//将2变为质数    scanf("%d",&n);    while(n>0)    {        printf("%d: %s\n",++m,A[n]?"no":"yes");        scanf("%d",&n);    }    return 0;}

原创粉丝点击