当前位置:网站首页>PAT 乙等 1013 C语言
PAT 乙等 1013 C语言
2022-06-23 04:11:00 【章鱼bro】
1013. 数素数 (20)
令Pi表示第i个素数。现任给两个正整数M <= N <= 104,请输出PM到PN的所有素数。
输入格式:
输入在一行中给出M和N,其间以空格分隔。
输出格式:
输出从PM到PN的所有素数,每10个数字占1行,其间以空格分隔,但行末不得有多余空格。
输入样例:5 27输出样例:
11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103
思路:从2开始,每遇到一个素数将计数变量加一,在此基础上判断是否大于M,是否小于N。
一、起始变量
1.M、N
2.count,素数计数变量
3.enterFlag换行计数变量,当此值为10时输出换行,并将enterFlag从新赋值为1
二、运算
1.输入M N
2.开始从2遍历,每遇到一个素数,计数变量加一,直到计数变量为N-1
3.计数变量加一以后,再判断是否大于M
3.再判断是否需要换行,若不需要在判断是否有空格
三、代码
//开始时间21:42
#include "stdio.h"
#include "math.h"
int isPrime(int n);
int main()
{
int M,N;
M = N = 0;
scanf("%d %d", &M, &N);
int i;
int count = 0;
int enterFlag = 1;
//开始从2遍历,每遇到一个素数,计数变量加一,直到计数变量为N-1
for(i = 2; count < N; i++)
{
if(isPrime(i))
{
count++;//计数变量加一以后,再判断是否大于M
if(count >= M)
{
if(enterFlag != 10)//再判断是否需要换行,若不需要在判断是否有空格
{
if(count == N)
{
printf("%d",i);
enterFlag++;
}
else
{
printf("%d ",i);
enterFlag++;
}
}
else
{
printf("%d\n",i);
enterFlag = 1;
}
}
}
}
return 0;
}
int isPrime(int n)
{
int ret = 1;
int i = 2;
if(n == 2)
{
return ret;
}
int s = sqrt(n);
for(i = 2; i <= s; i++)
{
if(n % i == 0)
{
ret = 0;
break;
}
}
return ret;
}边栏推荐
- 英集芯推出4串锂电池100W移动电源升降压方案SoC芯片IP5389
- 技术开发团队视角看到的数字藏品机遇与挑战
- mysql字符集
- 啊哈C语言 第7章 有了它你能做更多的事(第27-28讲)
- MySQL面试真题(二十五)——常见的分组比较场景
- AHA C language Chapter 8 game time is up (lesson 29)
- Differences between fs4059a and fs5080e charging chips
- STC 32-bit 8051 MCU development example tutorial I development environment construction
- Real MySQL interview question (XXVIII) -- case - Analysis of indicators of communication operators
- Wechat applet: Star Trek spaceship ticket production and generation
猜你喜欢

App automated test appium advanced

runc 符号链接挂载与容器逃逸漏洞预警(CVE-2021-30465)

sprintf 格式代码使用不规范在不同平台下的表现

The 510000 prize pool invites you to participate in the competition -- the second Alibaba cloud ECS cloudbuild developer competition is coming

Behind the hot digital collections, a strong technical team is needed to support the northern technical team

Win11 app store keeps turning around solution

Heimdall database proxy scale out 20 times

数字藏品赋能实体产业释放了哪些利好?

Wechat applet: a new interesting test

英集芯ip6806无线充电方案5W过Qi认证外围精简14颗器件
随机推荐
Opportunities and challenges of digital collections from the perspective of technology development team
Oracle exception
Yingjixin launched 4 series of lithium batteries 100W mobile power supply voltage rise and fall scheme SOC chip ip5389
STC 32比特8051單片機開發實例教程 一 開發環境搭建
What does the English letter PC mean? What does the Internet PC mean
MySQL面试真题(二十九)——案例-找到爱看的电影
Win11应用商店下载的软件怎么移到桌面
STC 32 Bit 8051 Single Chip Computer Development Example Tutorial one development environment
MySQL面试真题(二十四)——行列互换
C prime plus notes d'apprentissage - 2, constantes et formatage io (I / o)
Low cost 5W wireless charger scheme fs68001b simple charging chip
SIFT feature point extraction
How can digital collections empower economic entities?
[graduation season u; advanced technology Er] farewell to the confused self in the past two years. Regroup, junior I'm coming
MySQL面试真题(二十七)——RFM分析法对用户进行分类
Oracle异常
技能自检 | 想当测试Leader,这6项技能你会吗?
英集芯IP5566带TYPE-C口3A充放快充移动电源5w无线充二合一方案SOC
Win11 app store keeps turning around solution
ArcTime 制作中英文字幕视频