当前位置:网站首页>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 !
边栏推荐
- 实验九 输入输出流(节选)
- 优先队列PriorityQueue (大根堆/小根堆/TopK问题)
- Using spacedesk to realize any device in the LAN as a computer expansion screen
- Strengthen basic learning records
- Beautified table style
- FAQs and answers to the imitation Niuke technology blog project (I)
- 实验六 继承和多态
- 强化学习基础记录
- [MySQL database learning]
- Caching mechanism of leveldb
猜你喜欢
hashCode()与equals()之间的关系
Callback function ----------- callback
Read only error handling
Using spacedesk to realize any device in the LAN as a computer expansion screen
[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
甲、乙机之间采用方式 1 双向串行通信,具体要求如下: (1)甲机的 k1 按键可通过串行口控制乙机的 LEDI 点亮、LED2 灭,甲机的 k2 按键控制 乙机的 LED1
FAQs and answers to the imitation Niuke technology blog project (III)
.Xmind文件如何上传金山文档共享在线编辑?
FAQs and answers to the imitation Niuke technology blog project (I)
This time, thoroughly understand the MySQL index
随机推荐
js判断对象是否是数组的几种方式
Analysis of penetration test learning and actual combat stage
(原创)制作一个采用 LCD1602 显示的电子钟,在 LCD 上显示当前的时间。显示格式为“时时:分分:秒秒”。设有 4 个功能键k1~k4,功能如下:(1)k1——进入时间修改。
[面試時]——我如何講清楚TCP實現可靠傳輸的機制
[three paradigms of database] you can understand it at a glance
仿牛客技术博客项目常见问题及解答(一)
MySQL事务及实现原理全面总结,再也不用担心面试
A piece of music composed by buzzer (Chengdu)
[during the interview] - how can I explain the mechanism of TCP to achieve reliable transmission
Poker game program - man machine confrontation
Implementation of count (*) in MySQL
[the Nine Yang Manual] 2020 Fudan University Applied Statistics real problem + analysis
Intensive literature reading series (I): Courier routing and assignment for food delivery service using reinforcement learning
为什么要使用Redis
Record a penetration of the cat shed from outside to inside. Library operation extraction flag
力扣152题乘数最大子数组
Read only error handling
强化学习基础记录
【Numpy和Pytorch的数据处理】
这次,彻底搞清楚MySQL索引