我是靠谱客的博主 魁梧彩虹,这篇文章主要介绍[Loops]D. Liang 4.20 Printing Prime numbers between 2 and n.c[Loops]D. Liang 4.20 Printing Prime numbers between 2 and nDescriptionInputOutputNOTE:Sample InputSample Output,现在分享给大家,希望可以做个参考。
[Loops]D. Liang 4.20 Printing Prime numbers between 2 and n
Description
Write a program that reads an interger n, then print all the prime numbers between 2 and n, inclusively.
Input
An integer n (2<=n<=2000).
Output
All the prime number between 2 and n, inclusively.
You should display eight prime numbers per line, specify the width of each number’s print field to 5, justify the output to right.
NOTE:
- There is NO SPACE at the end of each line.
- There is A NEWLINE (n) at the end of last line iff it has eight number.
That is (. indicates <space> and ¬ indicates <newline>)
....2
or
....2....3....5....7...11...13...17...19¬
Sample Input
59
Sample Output
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
//
Date:2020/3/19
//
Author:xiezhg5
#include <stdio.h>
#include <math.h>
int main(void)
{
int count=0;
int i,j,k;
int m,n;
m=2;
scanf("%d",&n);
for(i=m;i<=n;i++)
{
k=sqrt(i);
//遍历2到√i的数即可
for(j=2;j<=k;j++)
if(i%j==0)
{
break;
//不是素数立即退出循环
}
if(j>=k+1)
{
if(i==1)
count=count;
else
{
printf("%5d",i);
count++;
if(count%8==0)
//注意输出要求
printf("n");
}
}
}
printf("n");
return 0;
}
最后
以上就是魁梧彩虹最近收集整理的关于[Loops]D. Liang 4.20 Printing Prime numbers between 2 and n.c[Loops]D. Liang 4.20 Printing Prime numbers between 2 and nDescriptionInputOutputNOTE:Sample InputSample Output的全部内容,更多相关[Loops]D.内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复