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 |
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;//根据题意,1和2不算素数 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 }