文章标题

来源:互联网 发布:xml json弹幕 编辑:程序博客网 时间:2024/06/05 06:57

Primes

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竟然不是素数,真不知道出题人怎么想的,最后注意的就是输出格式问题。下面附上代码段,希望各位大佬能够提供更好的解题思路。
#include<stdio.h>#include<math.h>int main(void){    int n;    int m;     int i = 2;    int test = 0;    while(scanf("%d",&n)!=EOF)    {        m = (int)sqrt(n);        if(n<=0)        break;        ++test;        for(i = 2;i <= m; i++)            if(n % i == 0)                break;        if(n==1||n==2)            printf("%d: %s\n",test,"no");        else if(i > m)            printf("%d: %s\n",test,"yes");        else            printf("%d: %s\n",test,"no");    }    return 0;} 
原创粉丝点击