当前位置:网站首页>实例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]
边栏推荐
- Working principle and type selection of common mode inductor
- leetcode - 445. 两数相加 II
- [paper reading] the latest transfer ability in deep learning: a survey in 2022
- Classic application of MOS transistor circuit design (2) - switch circuit design
- Embedded composition and route
- Hardware 1 -- relationship between gain and magnification
- H264 (I) i/p/b frame gop/idr/ and other parameters
- How to select conductive slip ring
- Let's briefly talk about the chips commonly used in mobile phones - OVP chips
- MHA High available Cluster for MySQL
猜你喜欢
Brief discussion on Buck buck circuit
STM32 single chip microcomputer -- debug in keil5 cannot enter the main function
Count the number of inputs (C language)
STM32 virtualization environment of QEMU
Hardware 3 -- function of voltage follower
Summary -st2.0 Hall angle estimation
Relationship between line voltage and phase voltage, line current and phase current
[cloud native | learn kubernetes from scratch] III. kubernetes cluster management tool kubectl
【云原生 | 从零开始学Kubernetes】三、Kubernetes集群管理工具kubectl
Measurement fitting based on Halcon learning [III] PM_ measure_ board. Hdev routine
随机推荐
动力电池UL2580测试项目包括哪些
Network communication model -- Network OSI tcp/ip layering
Compilation warning solution sorting in Quartus II
Network communication process
matlab timeserise
Halcon's practice based on shape template matching [1]
Process communication mode between different hosts -- socket
Sizeof (function name) =?
Weidongshan Internet of things learning lesson 1
Improve lighting C program
Shape template matching based on Halcon learning [VII] reuse_ model. Hdev routine
Create inf module in AMI code
Circleq of linked list
C WinForm [display real-time time in the status bar] - practical exercise 1
Bluetooth hc-05 pairing process and precautions
MySQL之MHA高可用集群
OLED 0.96 inch test
[paper reading] the latest transfer ability in deep learning: a survey in 2022
Design a clock frequency division circuit that can be switched arbitrarily
[cloud native | learn kubernetes from scratch] III. kubernetes cluster management tool kubectl