#include<stdio.h>
int main ()
{
int i,j,s,b,c;
printf("Input num:");
scanf("%d",&s);
if(s<2)
{
printf("Prime number is 2");
}
else if(s==2)
{
printf("Prime number is 3");
}
else
{
while(b==i)
{
i=s++;
for(j=2;j<i;j++)
if(i%j==0)break;
if(j=i)
b=i;
}
while(c==i)
{
i=s--;
for(j=2;j<i;j++)
if(i%j==0)break;
if(j=i)
c=i;
}
if((b-s)>(s-c))
{
printf("Prime number is %d",b);
}
else if((b-s)==(s-c))
{
printf("Prime number is %d %d",b,c);
}
else
{
printf("Prime number is %d",c);
}
}
return 0;
}
c程序新手 想问问这个问题怎么写?
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
2条回答 默认 最新
- threenewbee 2018-11-03 06:51关注
你的程序的循环错了,给你修改了下
// Q707861.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include<stdio.h> int main () { int i,j,s,b,c; printf("Input num:"); scanf("%d",&s); if(s<2) { printf("Prime number is 2\n"); } else if(s==2) { printf("Prime number is 3\n"); } else { i = s+1; while (true) { for(j=2;j<i;j++) if(i%j==0)break; if(j==i) { b=i; break; } i++; } i = s-1; while (true) { for(j=2;j<i;j++) if(i%j==0)break; if(j==i) { c=i; break; } i--; } if((b-s)<(s-c)) { printf("Prime number is %d\n",b); } else if((b-s)==(s-c)) { printf("Prime number is %d %d\n",c,b); } else { printf("Prime number is %d\n",c); } } return 0; }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 c程序不知道为什么得不到结果
- ¥40 复杂的限制性的商函数处理
- ¥15 程序不包含适用于入口点的静态Main方法
- ¥15 素材场景中光线烘焙后灯光失效
- ¥15 请教一下各位,为什么我这个没有实现模拟点击
- ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
- ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
- ¥20 有关区间dp的问题求解
- ¥15 多电路系统共用电源的串扰问题
- ¥15 slam rangenet++配置