当前位置:网站首页>实例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]
边栏推荐
- What are the test items of power battery ul2580
- Negative pressure generation of buck-boost circuit
- Explain task scheduling based on Cortex-M3 in detail (Part 1)
- DCDC circuit - function of bootstrap capacitor
- C WinForm [change the position of the form after running] - Practical Exercise 4
- Sql Server的存儲過程詳解
- My-basic application 2: my-basic installation and operation
- Shell script realizes the reading of serial port and the parsing of message
- Talk about the circuit use of TVs tube
- leetcode - 445. 两数相加 II
猜你喜欢

Step motor generates S-curve upper computer

Management and use of DokuWiki
![[trio basic tutorial 17 from getting started to mastering] set up and connect the trio motion controller and input the activation code](/img/58/576b6b77509ed7a9bef138f3899e37.jpg)
[trio basic tutorial 17 from getting started to mastering] set up and connect the trio motion controller and input the activation code

Explain task scheduling based on Cortex-M3 in detail (Part 2)

Network communication process

Shell script basic syntax
![[tutorial 15 of trio basic from introduction to proficiency] trio free serial communication](/img/08/7f28008a4aa999650998ba8dee5d8e.jpg)
[tutorial 15 of trio basic from introduction to proficiency] trio free serial communication

Solutions to compilation warnings in Quartus II

MySQL MHA high availability cluster

Explain task scheduling based on Cortex-M3 in detail (Part 1)
随机推荐
Explain task scheduling based on Cortex-M3 in detail (Part 1)
Ble encryption details
Shell script realizes the reading of serial port and the parsing of message
Take you to understand the working principle of lithium battery protection board
C # joint configuration with Halcon
Gradle复合构建
C WinForm [realize the previous and next selection pictures] - practice 7
STM32 virtualization environment of QEMU
Consul安装
MHA High available Cluster for MySQL
Naming rules for FreeRTOS
Improve lighting C program
Sql Server的存儲過程詳解
Network port usage
亿学学堂给的证券账户安不安全?哪里可以开户
QEMU demo makefile analysis
Explain task scheduling based on Cortex-M3 in detail (Part 2)
Measurement fitting based on Halcon learning [III] PM_ measure_ board. Hdev routine
What are the test items of power battery ul2580
动力电池UL2580测试项目包括哪些