新手村 Boss战-入门综合练习1 哥德巴赫猜想(升级版)
2017-12-08 10:42
302 查看
题目链接
欢迎加入“不会算法一群菜鸟”,群号是⑥⑥①⑨②2025,这是我设置的一道很低的门槛用来阻止广告的。入群的验证暗号是:我爱编译原理
题意理解
这就是随便写的,10分钟肯定能写完。代码
#include <cstring> #include <iostream> #include <cmath> #include <stdio.h> #include <stdlib.h> #include <string> #include <iomanip> #include <vector> using namespace std; /* * 素数筛选,判断小于MAXN的数是不是素数。 * notprime是一张表,为false表示是素数,true表示不是素数 */ const int MAXN = 20010; bool notprime[MAXN]; //值为false表示素数,值为true表示非素数 void init() { memset(notprime, false, sizeof notprime); notprime[0] = notprime[1] = true; for(int i = 2;i < MAXN; i++) { if(!notprime[i]) { if(i > MAXN/i) { continue; //防止后面i*i溢出(或者i,j用long long) } //直接从i*i开始就可以,小于i倍的已经筛选过了,注意是j+=i for(int j = i * i;j < MAXN;j += i) { notprime[j]=true; } } } } void read(int &x) { x = 0; int f = 1; char ch = getchar(); while(ch > '9'||ch < '0') { if(ch == '-') { f = -1; } ch = getchar(); } while(ch >= '0' && ch <= '9') { x = x * 10 + (int)(ch - 48); ch = getchar(); } x = x * f; } int main() { int n; read(n); init(); for(int i = 2; i < n; i++) { if(notprime[i]) { continue; } for(int j = 2; j < n; j++) { if(notprime[j]) { continue; } if(i + j >= n) { break; } if(!notprime[n - i - j]) { printf("%d %d %d", i, j, n - i - j); return 0; } } } return 0; }
欢迎加入“不会算法一群菜鸟”,群号是⑥⑥①⑨②2025,这是我设置的一道很低的门槛用来阻止广告的。入群的验证暗号是:我爱编译原理
相关文章推荐
- 洛谷-哥德巴赫猜想(升级版)-BOSS战-入门综合练习1
- 新手村 Boss战-入门综合练习1 陶陶摘苹果(升级版)
- 新手村 Boss战-入门综合练习1 三连击(升级版)
- 洛谷-陶陶摘苹果(升级版)-BOSS战-入门综合练习1
- 新手村 Boss战-入门综合练习1 烤鸡
- 新手村 Boss战-入门综合练习2 Cantor表
- 新手村 Boss战-入门综合练习2 小鱼会有危险吗
- 洛谷-三连击(升级版)-BOSS战-入门综合练习1
- 新手村之BOSS战-入门综合练习2
- 新手村 Boss站-入门综合练习2 Function
- 新手村之BOSS战-入门综合练习1
- 普及练习场 BOSS战-普及综合练习2 守望者的逃离
- js入门·对象的综合练习(重点介绍Link对象)
- 洛谷-烤鸡-BOSS战-入门综合练习1
- 15入门阶段综合练习
- js入门·对象的综合练习(重点介绍Link对象)
- UFT入门教程(4)—综合练习一
- 《Java从入门到放弃》JavaSE篇:综合练习——单身狗租赁系统(数组版)
- C语言编程入门——综合练习(一)
- BOSS战-普及综合练习3-积木大赛