当前位置:网站首页>实例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]
边栏推荐
- More than 90% of hardware engineers will encounter problems when MOS tubes are burned out!
- Live555 push RTSP audio and video stream summary (III) flower screen problem caused by pushing H264 real-time stream
- Interview catalogue
- PMSM dead time compensation
- Problem solving: interpreter error: no file or directory
- Charge pump boost principle - this article will give you a simple understanding
- Arduino uses nrf24l01+ communication
- Slist of linked list
- C WinForm [view status bar -- statusstrip] - Practice 2
- Relationship between line voltage and phase voltage, line current and phase current
猜你喜欢
![Shape template matching based on Halcon learning [VII] reuse_ model. Hdev routine](/img/55/0f05291755dc1c3c03db8e991a1ba1.jpg)
Shape template matching based on Halcon learning [VII] reuse_ model. Hdev routine

Negative pressure generation of buck-boost circuit

Wifi-802.11 negotiation rate table

Development tools -- gcc compiler usage

Shell script basic syntax

C language # and #

Arduino uses nrf24l01+ communication

MySQL之MHA高可用集群

Volatile of C language
![C WinForm [display real-time time in the status bar] - practical exercise 1](/img/9f/d193cbb488542cc4c439efd79c4963.jpg)
C WinForm [display real-time time in the status bar] - practical exercise 1
随机推荐
C language enhancement -- pointer
Network port usage
Circleq of linked list
C WinForm [exit application] - practice 3
【论文阅读】2022年最新迁移学习综述笔注(Transferability in Deep Learning: A Survey)
动力电池UL2580测试项目包括哪些
Zero length array in GNU C
Arduino uses nrf24l01+ communication
My-basic application 1: introduction to my-basic parser
Process communication mode between different hosts -- socket
Sql Server的存儲過程詳解
What are the test items of power battery ul2580
STM32 single chip microcomputer -- debug in keil5 cannot enter the main function
Step motor generates S-curve upper computer
Let's briefly talk about the chips commonly used in mobile phones - OVP chips
Summary of SIM card circuit knowledge
Connection mode - bridge and net
Semiconductor devices (III) FET
Imx6ull bare metal development learning 1-assembly lit LED
Shape template matching based on Halcon learning [VII] reuse_ model. Hdev routine