当前位置:网站首页>实例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]
边栏推荐
- WiFi wpa_ Detailed description of supplicant hostpad interface
- Summary of SIM card circuit knowledge
- After installing the new version of keil5 or upgrading the JLINK firmware, you will always be prompted about the firmware update
- How to select conductive slip ring
- On boost circuit
- Measurement fitting based on Halcon learning [II] meaure_ pin. Hdev routine
- Array integration initialization (C language)
- My-basic application 2: my-basic installation and operation
- [tutorial 19 of trio basic from introduction to proficiency] detailed introduction of trio as a slave station connecting to the third-party bus (anybus PROFIBUS DP...)
- Tailq of linked list
猜你喜欢

The firmware of the connected j-link does not support the following memory access

Communication standard -- communication protocol
![[trio basic from introduction to mastery tutorial XIV] trio realizes unit axis multi-color code capture](/img/c5/22c6148873508b9205972e1ad970a3.jpg)
[trio basic from introduction to mastery tutorial XIV] trio realizes unit axis multi-color code capture

C language # and #

Talk about the function of magnetic beads in circuits

Several important parameters of LDO circuit design and type selection

Nb-iot technical summary
![[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

STM32 single chip microcomputer - external interrupt

STM32 virtualization environment of QEMU
随机推荐
OC and OD gate circuit
STM32 single chip microcomputer - bit band operation
Step motor generates S-curve upper computer
Gradle composite construction
Interview catalogue
Explain task scheduling based on Cortex-M3 in detail (Part 2)
Classic application of MOS transistor circuit design (1) -iic bidirectional level shift
Improve lighting C program
C WinForm [change the position of the form after running] - Practical Exercise 4
[trio basic tutorial 17 from getting started to mastering] set up and connect the trio motion controller and input the activation code
Stablq of linked list
Let's briefly talk about the chips commonly used in mobile phones - OVP chips
Measurement fitting based on Halcon learning [III] PM_ measure_ board. Hdev routine
Take you to understand the working principle of lithium battery protection board
Nb-iot technical summary
Problem solving: interpreter error: no file or directory
Management and use of DokuWiki (supplementary)
Adaptive filter
Bootloader implementation of PIC MCU
Live555 push RTSP audio and video stream summary (I) cross compilation