Primes

xiaoxiao2021-03-01  92

题目:

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

水题,让你判断一个数是否为素数,而且这个数还非常小;

当然,这个题也是有坑的,那就是输入的数小于等于0就结束,我开始只看了样例输入输出,以为是输入0结束,结果一直不过

代码:

#include<stdio.h> using namespace std; int n,p[16000],k=0; void primary() {     p[1]=1;     for(int i=2;i<=16000;i++)     {         if(!p[i])         {             for(int j=2*i;j<=16000;j+=i)                 p[j]=1;         }     } } int main() {     int casee=0;     primary();     while(scanf("%d",&n)!=EOF)     {         if(n<=0)             break;         if(n==2)         {             printf("%d: no\n",++casee);             continue;         }         if(!p[n])             printf("%d: yes\n",++casee);         else              printf("%d: no\n",++casee);     }     return 0; }  

转载请注明原文地址: https://www.6miu.com/read-3861757.html

最新回复(0)