Primes
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9565 Accepted Submission(s): 4014
Problem Description
Write 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.
Input
Each 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.
Output
The 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 Input
1 2 3 4 5 17 0
Sample Output
1: no 2: no 3: yes 4: no 5: yes 6: yes
题意: 判断一个数是否为素数,本题 1 , 2 都不是素数 条件 n<=0 结束 程序
已AC代码:
#include<cstdio>
#include<cstring>
int prime[20000];
void into_prime()
{
memset(prime,0,sizeof(prime));
for(int i=2;i*i<=16000;++i)
{
if(prime[i]==0)
for(int j=i*i;j<=16000;j+=i)
prime[j]=1;
}
prime[0]=prime[1]=prime[2]=1;//本题 1 , 2 都不是素数
}
int main()
{
into_prime();
int n,CASE=1;
while(scanf("%d",&n)!=EOF)
{
if(n<=0)//条件 n<=0 结束
break;
printf("%d: ",CASE++);
if(prime[n]==0)
printf("yes\n");
else
printf("no\n");
}
return 0;
}