码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 2161 Primes(打表)

时间:2016-02-01 23:49:33      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:

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(打表)

标签:

原文地址:http://www.cnblogs.com/shenyw/p/5176517.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!