poj 2262(水题)
来源:互联网 发布:远程语音软件 编辑:程序博客网 时间:2024/06/01 07:39
代码如下
#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>bool is_prime[1000005];int prime[1000005];using namespace std;int main(){memset(prime,0,sizeof(prime));int p=0;for(int i=0;i<=1000000;i++) is_prime[i]=true;is_prime[0]=false; is_prime[1]=false;for(int i=2;i<=1000000;i++){if(is_prime[i]){prime[p++]=i;for(int j=2*i;j<=1000000;j+=i) is_prime[j]=false;}}int n;while(scanf("%d",&n)&&n!=0){int end;for(int i=0;i<p;i++) {if(prime[i]>=n) {end=i-1;break;}}int is_end=0;for(int i=end;i>=0;i--){for(int j=0;j<=i;j++){if(prime[i]+prime[j]>n) break;if(prime[i]+prime[j]==n) {printf("%d = %d + %d\n",n,prime[j],prime[i]);is_end=1;break;}}if(is_end==1) break; } if(is_end==0) printf("Goldbach's conjecture is wrong.\n"); }}
不过处理的时候写麻烦了
既可以用判断素数也可以埃氏筛法
关键在于
- j = n-i;
- if(prime(j) && prime(i))
- {
- cout << n <<" = " << i << " + " << j << endl;
- break;
- }
阅读全文
0 0
- poj 2262(水题)
- poj 2262
- POJ-2262
- poj 2262
- POJ 2262
- poj 2262
- poj 2262
- poj-2262
- POJ 2262
- POJ 2262
- POJ 2262
- poj 2262
- POJ 2262
- poj水题
- POJ 水题
- POJ 水题
- poj水题
- poj 水题
- Echarts tooltip 自定义 css 样式 带小三角的背景样式
- yii基本添加,修改,删除,查询db语句
- SAP ABAP常用增强记录文档
- 遗传算法 与旅行商问题
- Siri真的要离开我们了?
- poj 2262(水题)
- BAT批处理定期删除清理指定N天前的文件
- 大型网站架构演化过程
- React native 环境
- Python3.X之函数式编程笔记
- A quick-and-dirty audio sample mixing technique to avoid clipping
- 定义宏可以方便地访问stl容器
- MyISAM 和InnoDB
- mac php5.6+oci8+pdo_oci扩展