当前位置:网站首页>实例005:三数排序 输入三个整数x,y,z,请把这三个数由小到大输出。
实例005:三数排序 输入三个整数x,y,z,请把这三个数由小到大输出。
2022-07-05 08:16:00 【懒笑翻】
实例005:三数排序
题目:输入三个整数x,y,z,请把这三个数由小到大输出。
方法一:
# 循环遍历每一个数字,两个数两个数对比,前一个数如果大于后一个数则两个数互换位置
# 输入三个数存到raw中
raw = []
for i in range(3):
x = int(input('int%d: ' % (i + 1)))
raw.append(x)
# 循环遍历每一个数字,两个数两个数对比,前一个数如果大于后一个数则两个数互换位置
for i in range(len(raw)):
for j in range(i, len(raw)):
if raw[i] > raw[j]:
raw[i], raw[j] = raw[j], raw[i]
print(raw)
int1: 3
int2: 2
int3: 6
[2, 3, 6]
方法二:
直接调用排序函数sorted()
# 输入三个数存到raw2中
raw2 = []
for i in range(3):
x = int(input('int%d: ' % (i + 1)))
raw2.append(x)
# 直接调用函数排序
print(sorted(raw2))
int1: 5
int2: 7
int3: 3
[3, 5, 7]
边栏推荐
- Problem solving: interpreter error: no file or directory
- [trio basic tutorial 17 from getting started to mastering] set up and connect the trio motion controller and input the activation code
- Anonymous structure in C language
- Bluetooth hc-05 pairing process and precautions
- Programming knowledge -- basis of C language
- Use indent to format code
- [cloud native | learn kubernetes from scratch] III. kubernetes cluster management tool kubectl
- Simple design description of MIC circuit of ECM mobile phone
- Working principle and type selection of common mode inductor
- Circleq of linked list
猜你喜欢
Programming knowledge -- basis of C language
Improve lighting C program
Wifi-802.11 negotiation rate table
Why is 1900 not a leap year
Compilation warning solution sorting in Quartus II
Several implementation schemes of anti reverse connection protection of positive and negative poles of power supply!
Various types of questions judged by prime numbers within 100 (C language)
[trio basic tutorial 18 from introduction to proficiency] trio motion controller UDP fast exchange data communication
Stablq of linked list
Shape template matching based on Halcon learning [viii] PM_ multiple_ models. Hdev routine
随机推荐
After installing the new version of keil5 or upgrading the JLINK firmware, you will always be prompted about the firmware update
Network communication model -- Network OSI tcp/ip layering
UE像素流,来颗“减肥药”吧!
Summary of SIM card circuit knowledge
C WinForm [get file path -- traverse folder pictures] - practical exercise 6
MHA High available Cluster for MySQL
Arduino uses nrf24l01+ communication
VESC Benjamin test motor parameters
Management and use of DokuWiki
Gradle复合构建
Interview catalogue
亿学学堂给的证券账户安不安全?哪里可以开户
Improve lighting C program
How to select conductive slip ring
DokuWiki deployment notes
Sql Server的存儲過程詳解
Create inf module in AMI code
Charge pump boost principle - this article will give you a simple understanding
C WinForm [view status bar -- statusstrip] - Practice 2
[cloud native | learn kubernetes from scratch] III. kubernetes cluster management tool kubectl