博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
最近素数问题——C语言
阅读量:4676 次
发布时间:2019-06-09

本文共 698 字,大约阅读时间需要 2 分钟。

从键盘输入一个整数,输出距离该数最近的素数 

#include
#include
int judge(int x) { //判断素数 if (x < 2) return 0; for (int i = 2; i <= sqrt(x); i++) if (x % i == 0) return 0; return 1;}int main() { int s, left, right, flag = 0; int count = 0; //记录个数 int result[2]; //记录结果 printf("Input num:"); scanf("%d", &s); left = right = s; while (flag == 0) { if (left>0&&judge(--left)) { //左边小于0时就不用再判断素数了 result[count++] = left; flag = 1; } if (judge(++right)) { result[count++] = right; flag = 1; } } if (count == 1) printf("Prime number is %d", result[0]); else printf("Prime number are %d,%d", result[0], result[1]); return 0;}

 

转载于:https://www.cnblogs.com/F-itachi/p/9974339.html

你可能感兴趣的文章
Day3 02判定两个对象的引用是否相同
查看>>
极光推送
查看>>
PCB MongoDB 监控
查看>>
springmvc接收JSON类型的数据
查看>>
mybatis配置文件详解
查看>>
Objective-C plist文件与KVC 的使用
查看>>
jqGrid(2)
查看>>
杂题 UVAoj 107 The Cat in the Hat
查看>>
关于jquery-weui.js中时间控件datetimepicker的使用
查看>>
单页面应用程序(SPA)的优缺点
查看>>
http请求和http响应详细解析
查看>>
Centos 配置eth0 提示Device does not seem to be present
查看>>
OS开发入门教程(1)
查看>>
arduino 驱动电调
查看>>
一个游标的性能问题
查看>>
JMeter学习-2 JMeter环境搭建
查看>>
SQL SERVER 2012疑难问题解决方法
查看>>
关于Android RenderScript 的详细说明和一些实用文档
查看>>
POJ1051 P,MTHBGWB
查看>>
士兵队列训练问题
查看>>