素数

来源:互联网 发布:it服装官网 编辑:程序博客网 时间:2024/06/04 19:08

九度1047:素数判定 题目地址:http://ac.jobdu.com/problem.php?pid=1047

题目描述:

给定一个数n,要求判断其是否为素数(0,1,负数都是非素数)。

输入:

测试数据有多组,每组输入一个数n。

输出:

对于每组输入,若是素数则输出yes,否则输入no。

样例输入:
13

样例输出:

yes
#include<iostream>#include<cmath>using namespace std; bool isPrime(int x){    //0,1和负数都是非素数 ;2是素数     if(x <= 0 || x == 1) return 0;    else if(x == 2) return 1;    else{        int tmp = 2;        while(tmp < sqrt(x + 1)){            if(x % tmp == 0) return 0;            tmp++;        }       }    return 1;} int main(){    int n;    while(cin >> n){        if(isPrime(n)) cout << "yes" << endl;        else cout << "no" << endl;    }         return 0;}  /**************************************************************    Problem: 1047    User: cherish    Language: C++    Result: Accepted    Time:0 ms    Memory:1532 kb****************************************************************/


九度1163:素数 题目地址:http://ac.jobdu.com/problem.php?pid=1163
题目描述:

输入一个整数n(2<=n<=10000),要求输出所有从1到这个整数之间(不包括1和这个整数)个位为1的素数,如果没有则输出-1。

输入:

输入有多组数据。
每组一行,输入n。

输出:

输出所有从1到这个整数之间(不包括1和这个整数)个位为1的素数(素数之间用空格隔开,最后一个素数后面没有空格),如果没有则输出-1。

样例输入:
100
样例输出:
11 31 41 61 71
#include<iostream>#include<cmath>using namespace std; bool isPrime(int x){    //0,1和负数都是非素数 ;2是素数     if(x <= 0 || x == 1) return 0;    else if(x == 2) return 1;    else{        int tmp = 2;        while(tmp < sqrt(x + 1)){            if(x % tmp == 0) return 0;            tmp++;        }       }    return 1;} int main(){    int n,i,cnt;    while(cin >> n){        cnt = 0;        for(i = 2;i < n;i++){            //个位数为1且是素数则输出             if(i % 10 == 1 && isPrime(i)){                if(cnt == 0) cout << i;                else cout << " " << i;                cnt++;            }           }        if(cnt == 0) cout << -1;        cout << endl;    }         return 0;}  /**************************************************************    Problem: 1163    User: cherish    Language: C++    Result: Accepted    Time:10 ms    Memory:1532 kb****************************************************************/

九度1040:prime number 题目地址:http://ac.jobdu.com/problem.php?pid=1040
题目描述:

Output the k-th prime number.

输入:

k≤10000

输出:

The k-th prime number.

样例输入:
37
样例输出:
517
#include <iostream>#include <string>using namespace std; /***用之前的单个判断法超时,所以采用筛选法 *筛选法的原理是以空间换时间*可以定义一个数组,数组的第i个元素如果是0则表示i不是素数*如果数组第i个元素是1则表示i是素数 **/#define N 200000int prime[N + 1];    //初始化该数组:预先处理出相关区间内的所有素数 void initprime(){    int i;    //首先将数组全部初始化为1     for(i = 0;i <= N;i++) prime[i] = 1;    //0和1都不是素数     prime[0] = prime[1] = 0;    for(i = 2;i <= N;i++){        //如果该元素是素数,它的倍数都标为0         if(prime[i] == 1){            for(int j = 2;i * j <= N;j++){                prime[i * j] = 0;            }           }    }   }  int main(){    initprime();    int k,i,cnt;    while(cin >> k){        cnt = 0;        for(i = 0;i <= N && cnt < k;i++){            if(prime[i] == 1) cnt++;        }        i--;        cout << i << endl;      }}/**************************************************************    Problem: 1040    User: cherish    Language: C++    Result: Accepted    Time:10 ms    Memory:2300 kb****************************************************************/

九度1440:Goldbach's Conjecture 题目地址:http://ac.jobdu.com/problem.php?pid=1440
题目描述:

Goldbach's Conjecture: For any even number n greater than or equal to 4, there exists at least one pair of prime numbers p1 and p2 such that n = p1 + p2. 
This conjecture has not been proved nor refused yet. No one is sure whether this conjecture actually holds. However, one can find such a pair of prime numbers, if any, for a given even number. The problem here is to write a program that reports the number of all the pairs of prime numbers satisfying the condition in the conjecture for a given even number.

A sequence of even numbers is given as input. Corresponding to each number, the program should output the number of pairs mentioned above. Notice that we are interested in the number of essentially different pairs and therefore you should not count (p1, p2) and (p2, p1) separately as two different pairs.

输入:

An integer is given in each input line. You may assume that each integer is even, and is greater than or equal to 4 and less than 2^15. The end of the input is indicated by a number 0.

输出:

Each output line should contain an integer number. No other characters should appear in the output.

样例输入:
610120
样例输出:
121
#include<iostream>using namespace std; //2的15次方 = 32768 #define N 40000int prime[N + 1];    //初始化该数组:预先处理出相关区间内的所有素数 void initprime(){    int i;    for(i = 0;i <= N;i++) prime[i] = 1;    prime[0] = prime[1] = 0;    for(i = 2;i <= N;i++){        if(prime[i] == 1){            for(int j = 2;i * j <= N;j++){                prime[i * j] = 0;            }           }    }   }  int  main(){    initprime();    int num,i,mid,cnt;    while(cin >> num){        mid = num / 2;        cnt = 0;        if(num == 0) break;        for(i = 2;i <= mid;i++){            if(prime[i] == 1 && prime[num - i] == 1) cnt++;        }        cout << cnt << endl;    }         return 0;}  /**************************************************************    Problem: 1440    User: cherish    Language: C++    Result: Accepted    Time:10 ms    Memory:1676 kb****************************************************************/













0 0