POJ2262
来源:互联网 发布:易天行定位软件 编辑:程序博客网 时间:2024/04/27 18:16
问题描述:哥德巴赫猜想——给定一个n(6 <= n < 1000000),找出所有可能的两个质数和恰好为n的组合。
解题思路:从最小的质数2开始依次寻找,利用isPrime()函数判断所给的数是否为质数,最后判断满足条件则输出组合。
code(c)
#include<stdio.h>#include<math.h>bool isPrime(int num){ int m; if(num==1) return false; if(num==2) return true; for(m=2;m<(int)sqrt((double)num);m++){ if(num%m==0) break; } if(num%m==0) return false; else return true;}int main(){ int a,i,s,flag; while(1){ flag=1; scanf("%d",&a); if(a==0) return 0; for(i=2;i<=a/2;i++) if(isPrime(i)){ s=a-i; if(isPrime(s)){ flag=0; printf("%d = %d + %d\n",a,i,s); break; } } if(flag) printf("Goldbach's conjecture is wrong.\n"); } return 0;}
0 0
- POJ2262
- poj2262
- poj2262
- poj2262
- poj2262
- poj2262
- poj2262
- POJ2262
- poj2262
- poj2262
- POJ2262
- poj2262
- poj2262
- poj2262
- POJ2262
- Poj2262
- poj2262 Goldbach's Conjecture
- POJ2262 Goldbach's Conjecture
- Unix Socket Tutorial Intro
- 国王的魔镜
- 理解transform
- IDEA实用插件
- android studio与git
- POJ2262
- RANSAC及其改进
- listview 嵌套listview的显示问题
- <%@ include file=” ”%> ——最简洁易懂的解释
- Mysql5.7安装配置
- tomcat虚拟路径无法访问中文路径
- javascript实现两个select选择框内容动态绑定
- socket与tcp/ip编程
- ubuntu下安装redis