POJ
来源:互联网 发布:网络摄像机组装配件 编辑:程序博客网 时间:2024/06/09 08:33
给出一个数字,然后用两个素数的和来表示这个数n;
先筛选素数,打表,然后遍历,从3开始,每次加2,因为偶数一定不是素数;
然后用n减去i,去判定是不是素数,不需要枚举;
#include<iostream>#include<cstring>#include<cmath>#define MAXN 1000005using namespace std;bool isprime[MAXN];void checkprime(int n) {//快速打表 memset(isprime, true, sizeof(isprime)); isprime[1] = false;//把1筛除 for(int i = 3; i <= n; i+=2) { if(isprime[i]) { for(int j=i+i; j <= n; j+=i) {//筛除以i为倍数的数字(一定不是素数) isprime[j] = false; } } }}int main () { int n; checkprime(MAXN); while(cin >> n && n) { int m = n, flag = 1; for(int i = 3; i <= m; i+=2) {//遍历,注意i+=2 if(isprime[i] && isprime[n-i]) { printf("%d = %d + %d\n", n, i, n-i); flag = 0; break; } } if(flag) puts("Goldbach's conjecture is wrong."); } return 0;}
阅读全文
0 0
- POJ
- poj
- POJ
- POJ
- poj
- poj
- POJ
- POJ
- poj
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- 【Tensorflow】tf.placeholde函数解释与用法
- 图解HTTPS协议加密解密全过程
- 【牛客】星际穿越
- kubernetes init-container
- UVALive
- POJ
- LeetCode 205. Isomorphic Strings
- 230. Kth Smallest Element in a BST
- Combination Sum
- bzoj 3107: [cqoi2013]二进制a+b 数位dp
- C++实现复数Complex
- 认识HTML5的WebSocket
- Leetcode之Spiral Matrix II 问题
- 【LeetCode算法练习(C++)】Regular Expression Matching