hdu 2161 Primes

http://acm.hdu.edu.cn/showproblem.php?pid=2161

Primes

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
很水的一道判断素数的问题,只是要注意,在这道题中,如果输入的是2的话,也要输出no,还有就是当输入的 n<=0 时结束

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;

#define N 100005
#define INF 0x3f3f3f3f
#define PI acos (-1.0)
#define EPS 1e-8
#define met(a, b) memset (a, b, sizeof (a))

int isprime[N] = {1, 1, 0}, a[N];

void prime ()
{
    for (int i=2; i<=N; i++)
    {
        if (!isprime[i])
        {
            for (int j=i+i; j<=N; j+=i)
                isprime[j] = 1;
        }
    }
}

int main ()
{
    int n, k = 1;
    prime ();
    while (scanf ("%d", &n) !=EOF)
    {
        if (n <= 0) break;

        if (n == 2 || isprime[n])
            printf ("%d: no\n", k++);
        else if (!isprime[n])
            printf ("%d: yes\n", k++);
    }
    return 0;
}


你可能感兴趣的:(数论基础)