HDU 2161 Primes(打表)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 2161 Primes(打表)相关的知识,希望对你有一定的参考价值。

Primes

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 9888    Accepted Submission(s): 4136

 

 

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

 

 

Source

2008 “Sunline Cup” National Invitational Contest - Warm Up

 

 

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 bool a[16010];
 6 void p()
 7 {
 8     a[1]=a[2]=false;  
 9     int tmp;  
10     for(int i=3;i<16010;i++){  
11         tmp=(i+1)/2;  
12         for(int j=2;j<=tmp;j++){  
13             if(i%j==0){  
14                 a[i]=false;  
15                 break;  
16             }  
17         }  
18     }  
19 }
20 int main()
21 {
22     int n;
23     int j=0;
24     memset(a,true,sizeof(a));
25     p();
26     while(cin>>n&&n>0){
27         if(a[n]) cout<<++j<<": yes"<<endl;
28         else cout<<++j<<": no"<<endl;
29     }
30     return 0;
31 }

 

以上是关于HDU 2161 Primes(打表)的主要内容,如果未能解决你的问题,请参考以下文章

HDU - 2161 - Primes (质数)

HDU 2161 Primes (素数筛选法)

HduOJ 2162 - Primes

SPOJ Distinct Primes 打表

UVA1213Sum of Different Primes(素数打表 + DP)

hdu 5901 Count primes