HDU1397 POJ2909 UVA686 UVALive5674 Goldbach's Conjecture(II)
来源:互联网 发布:淘宝直通车点击 编辑:程序博客网 时间:2024/06/15 06:29
Regionals 1998 >> Asia - Tokyo
问题链接:HDU1397 POJ2909 UVA686 UVALive5674 ZOJ1657 Goldbach's Conjecture(II)。
这个问题是验证哥德巴赫猜想,对于输入的n,计算和等于n的奇素数对的个数。
不预先打表时间上会超时。
这个问题,HDU1397和UVA686的测试数据是不一样的。最初做的程序,HDU中AC,而UVA中WA。估计原因是UVA868中的n是包含奇数的,例如7=2+5。所以后来写的程序将计算素数对的循环做了修改,程序通过了。
这里分别给出C语言程序和C++语言程序的两个版本。
AC的C语言程序如下:
/* HDU1397 POJ2909 UVA686 Goldbach's Conjecture(II) */#include <stdio.h>#include <math.h>#define MAXN 32767int prime[MAXN+2] = {0, 0, 1, 1, 0};// 试除法判断一个数是否为素数int isprime(int n){ int end2, i; end2 = sqrt(n); for(i=3; i<=end2; i+=2) { if(n % i == 0) break; } return i > end2 ? 1 : 0;}void maketable(int n){ int i; for(i=5; i<n; i+=2) { prime[i] = isprime(i); prime[i+1] = 0; }}int main(void){ int n; int count, i; // 打表 maketable(MAXN); while(scanf("%d", &n) != EOF) { // 判定结束条件 if(n == 0) break; // 计算素数对个数 count = 0; for(i=2; i<=n/2; i++) if(prime[i] && prime[n-i]) count++; // 输出结果 printf("%d\n", count); } return 0;}
AC的C++语言程序如下:
/* HDU1397 POJ2909 UVA686 UVALive5674 Goldbach's Conjecture(II) */#include <iostream>#include <cmath>#include <cstdio>using namespace std;const int MAXN = 2000000;bool prime[MAXN+1] = {false, false, true};// 埃氏筛选法void esieve(bool sflag[], int n){ // 初始化 for(int i=3; i<=n; i++) { sflag[i++] = true; sflag[i] = false; } // 筛选 int max = sqrt(n); for(int i=3; i<=max; i++) { if(sflag[i]) { for(int j=i+i; j <= n; j+=i) sflag[j] = false; } }}int main(){ esieve(prime, MAXN); int n, count, i; while(scanf("%d", &n) != EOF) { // 判定结束条件 if(n == 0) break; // 计算素数对个数 count = 0; for(i=2; i<=n/2; i++) if(prime[i] && prime[n-i]) count++; // 输出结果 printf("%d\n", count); } return 0;}
HDU中AC,而UVA中WA的C语言程序如下:
/* HDU1397 POJ2909 UVA686 Goldbach's Conjecture(II) */#include <stdio.h>#include <math.h>#define MAXN 32767int prime[MAXN+2] = {0, 0, 1, 3, 0};// 试除法判断一个数是否为素数int isprime(int n){ int end2, i; end2 = sqrt(n); for(i=3; i<=end2; i+=2) { if(n % i == 0) break; } return i > end2 ? 1 : 0;}void maketable(int n){ int i; for(i=5; i<n; i+=2) { prime[i] = isprime(i); prime[i+1] = 0; }}int main(void){ int n; int count, i; // 打表 maketable(MAXN); while(scanf("%d", &n) != EOF) { // 判定结束条件 if(n == 0) break; // 计算素数对个数 count = 0; for(i=3; i<=n/2; i+=2) if(prime[i] && prime[n-i]) count++; // 输出结果 printf("%d\n", count); } return 0;}
1 0
- HDU1397 POJ2909 UVA686 UVALive5674 Goldbach's Conjecture(II)
- HDU1397:Goldbach's Conjecture
- POJ2909:Goldbach's Conjecture
- poj2909 Goldbach's Conjecture
- POJ2909 Goldbach's Conjecture
- poj2909——Goldbach's Conjecture
- UVa 686 - Goldbach's Conjecture (II)
- Goldbach's Conjecture
- poj2262 Goldbach's Conjecture
- Goldbach's Conjecture
- Goldbach's Conjecture
- HOJ1459 Goldbach's Conjecture
- POJ2262 Goldbach's Conjecture
- 2262 Goldbach's Conjecture
- Goldbach's Conjecture
- POJ2262:Goldbach's Conjecture
- Goldbach's Conjecture
- Goldbach's Conjecture
- spring配置事务处理的五种方式
- 基于双卡尔曼滤波算法的电池SOC估计
- 安装Vm12
- Ubuntu下配置Eclipse调试环境
- thingking in java2.11练习(3)
- HDU1397 POJ2909 UVA686 UVALive5674 Goldbach's Conjecture(II)
- HDU2818加权并查集
- QTableWidget的用法总结
- [李景山php]thinkphp核心源码注释|Apc.class.php
- poj 3278 || hdu2717 Catch That Cow 人生第一道BFS
- 简单功能强大的jQuery图片剪裁插件Image Cropper
- [李景山php]thinkphp核心源码注释|Db.class.php
- 2.IOS>UI>UIView
- day2 HDU 2199 Can you solve this equation?