当前位置:网站首页>7-1 output all primes between 2 and n (PTA programming)
7-1 output all primes between 2 and n (PTA programming)
2022-07-06 13:55:00 【Programming Lindaiyu】
This problem requires output 2
To n
All primes between , Output per row 10
individual . Prime numbers can only be 1 A positive integer divided by itself . Be careful :1 Not primes ,2 Prime number .
Input format :
Input gives an integer in a long integer range in one line .
Output format :
Output prime number , Each number accounts for 6 position , Output per row 10 individual . If the number of primes output in the last line is less than 10 individual , You also need a new line .
sample input :
10
sample output :
2 3 5 7
Code (Python):
n=int(input()) # First enter n, because input The input is a string , So it needs to be converted into int type
x=0 # Used to record the number of primes output
if n==2: # A special case : Input n be equal to 2( Because the following cycle needs to be judged n Is it divisible 2~n-1 Number between , If you will 2 Put it in the following loop , Because it can divide 2, Therefore, it will be judged as a non prime number and cause errors
print("%6d"%n,end='') # Note the output format ,%6d Indicates that each number accounts for 6 digit ,end='' Means no carriage return at the end
elif n>2: # General situation
for i in range(2,n): # Start judging 2~n( barring n) Between each number
j=2 # Divide each number by 2
while i%j!=0: # If divided by j Not for 0, Into the loop ,j Keep adding 1, Until you exit the loop
j+=1
if j==i: # Judge to exit above while Reasons for the cycle , If it is j be equal to i, It shows that the number has been added to itself without encountering a divisible number
if x!=0 and x%10==0: # See how many numbers it is output in this line , To judge whether to change lines :x be equal to 0 It is the first number of output , Don't wrap :x Remainder 10 be equal to 0 Words , Indicates that the output number is 10 Multiple , To wrap
x+=1 # One number per output ,x Add one
print() # Because the number of outputs is 10 Multiple , Indicates that the line outputs 10 Number , To wrap (print The default line breaks )
print("%6d"%i,end='') # Output primes in format
else: # Otherwise, it is not the above two situations , Indicates that the number of lines is not full 10 individual , There is no need to wrap , Normal output
x+=1 # One number per output ,x Add one
print("%6d"%i,end='') # Output primes in format
The above program gives more detailed comments , For novice Xiaobai's reference . The thinking design of the program is not optimal , yes “ Stupid way ”, You are welcome to correct your mistakes or give better ideas .
I am a rookie who wants to be Kunpeng , Everyone's encouragement is my driving force , Welcome to like collection comments !
边栏推荐
- String ABC = new string ("ABC"), how many objects are created
- 【Numpy和Pytorch的数据处理】
- Read only error handling
- Nuxtjs quick start (nuxt2)
- Using spacedesk to realize any device in the LAN as a computer expansion screen
- [the Nine Yang Manual] 2018 Fudan University Applied Statistics real problem + analysis
- ABA问题遇到过吗,详细说以下,如何避免ABA问题
- 【MySQL数据库的学习】
- hashCode()与equals()之间的关系
- 抽象类和接口的区别
猜你喜欢
C language Getting Started Guide
仿牛客技术博客项目常见问题及解答(三)
SRC挖掘思路及方法
Strengthen basic learning records
Have you encountered ABA problems? Let's talk about the following in detail, how to avoid ABA problems
This time, thoroughly understand the MySQL index
[面试时]——我如何讲清楚TCP实现可靠传输的机制
这次,彻底搞清楚MySQL索引
仿牛客技术博客项目常见问题及解答(一)
Using spacedesk to realize any device in the LAN as a computer expansion screen
随机推荐
[data processing of numpy and pytoch]
关于双亲委派机制和类加载的过程
这次,彻底搞清楚MySQL索引
[dark horse morning post] Shanghai Municipal Bureau of supervision responded that Zhong Xue had a high fever and did not melt; Michael admitted that two batches of pure milk were unqualified; Wechat i
【数据库 三大范式】一看就懂
It's never too late to start. The tramp transformation programmer has an annual salary of more than 700000 yuan
7-8 7104 约瑟夫问题(PTA程序设计)
The difference between cookies and sessions
[insert, modify and delete data in the headsong educator data table]
7-7 7003 组合锁(PTA程序设计)
Strengthen basic learning records
7-9 制作门牌号3.0(PTA程序设计)
About the parental delegation mechanism and the process of class loading
fianl、finally、finalize三者的区别
【黑马早报】上海市监局回应钟薛高烧不化;麦趣尔承认两批次纯牛奶不合格;微信内测一个手机可注册俩号;度小满回应存款变理财产品...
This time, thoroughly understand the MySQL index
【VMware异常问题】问题分析&解决办法
Read only error handling
[the Nine Yang Manual] 2016 Fudan University Applied Statistics real problem + analysis
FAQs and answers to the imitation Niuke technology blog project (II)