当前位置:网站首页>刷题-洛谷-P1304 哥德巴赫猜想
刷题-洛谷-P1304 哥德巴赫猜想
2022-08-04 19:44:00 【宋向上_UP】
P1304 哥德巴赫猜想-C语言
1、题目
2、解题过程
结果:
代码:
//洛谷 P1304 哥德巴赫猜想
#include <stdio.h>
//求质数
int prime(int a) {
int i;
if (a == 2) {
return 1;//表示是质数
}
for (i = 2; i < a; i++) {
//辗转相除
if (a % i == 0) {
break;
}
}
if (i >= a) {
return 1;//表示是质数
}
else {
return 0;//表示不是质数
}
}
int main() {
int n;//输入的偶数N 4≤N≤10000
int i, j;
int temp;
//int flag=0;
int p_1, p_2;//用于接收是否为质数
scanf_s("%d", &n);//正偶数
for (i = 4; i <= n; i = i + 2) {
//偶数
//flag = 0;
for (j = 2; j < i; j++) {
/* if (flag == 1) { break; }*/
temp = i - j;
p_1 = prime(j);
p_2 = prime(temp);
if (p_1 == 1 && p_2 == 1) {
//都是质数
if (i == n) {
printf("%d=%d+%d", i, j, temp);
}
else {
printf("%d=%d+%d\n", i, j, temp);
}
//flag = 1;
break;
}
}
}
return 0;
}
边栏推荐
猜你喜欢
随机推荐
如何推动乡村振兴的落地
VQ Realization of Wavelet Extraction Features
The establishment of simple data cache layer
手把手教你CSP系列之script-src
Aura clock chip generation configuration file script
Switch node version and switch npm source tool
宏定义小方法
The Development and Current Situation of Object Detection
基于HDF的LED驱动程序开发(2)
Use "green computing" technology to promote sustainable development of computing power
笔记本WIFI无法上网(无Internet访问权限)
从卖产品到卖“链路”:20条策略 解读直播带货迭代玩法
visual studio 与 visual studio code
电脑一键重装系统内存完整性无法打开怎么办
Embrace the Cmake child is simple and practical, but inflexible
二叉树的遍历
Exploration and Practice of Database Governance
PostgreSQL的 SPI_接口函数
对比几类主流的跨端技术方案
zynq records