当前位置:网站首页>Example 005: three numbers sorting input three integers x, y, Z, please output these three numbers from small to large.
Example 005: three numbers sorting input three integers x, y, Z, please output these three numbers from small to large.
2022-07-05 08:22:00 【Lazy smile】
example 005: Three number order
subject : Enter three integers x,y,z, Please output these three numbers from small to large .
Method 1 :
# Loop through each number , Compare two numbers , If the former number is greater than the latter number, the two numbers will exchange positions
# Enter three numbers and save to raw in
raw = []
for i in range(3):
x = int(input('int%d: ' % (i + 1)))
raw.append(x)
# Loop through each number , Compare two numbers , If the former number is greater than the latter number, the two numbers will exchange positions
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]
Method 2 :
Call the sort function directly sorted()
# Enter three numbers and save to raw2 in
raw2 = []
for i in range(3):
x = int(input('int%d: ' % (i + 1)))
raw2.append(x)
# Direct call function sorting
print(sorted(raw2))
int1: 5
int2: 7
int3: 3
[3, 5, 7]
边栏推荐
- Use indent to format code
- Sizeof (function name) =?
- MySQL之MHA高可用集群
- STM32 single chip microcomputer -- volatile keyword
- 剑指 Offer 05. 替换空格
- Introduction of air gap, etc
- Hardware 3 -- function of voltage follower
- Some thoughts on extracting perspectives from ealfa and Ebeta
- Shape template matching based on Halcon learning [9] PM_ multiple_ dxf_ models. Hdev routine -- [read and write XLD from DXF file]
- List of linked lists
猜你喜欢
实例001:数字组合 有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少?
实例006:斐波那契数列
Various types of questions judged by prime numbers within 100 (C language)
Compilation warning solution sorting in Quartus II
剑指 Offer 05. 替换空格
Explain task scheduling based on Cortex-M3 in detail (Part 1)
STM32 summary (HAL Library) - DHT11 temperature sensor (intelligent safety assisted driving system)
Measurement fitting based on Halcon learning [III] PM_ measure_ board. Hdev routine
实例004:这天第几天 输入某年某月某日,判断这一天是这一年的第几天?
Measurement fitting based on Halcon learning [i] fuse Hdev routine
随机推荐
STM32 --- NVIC interrupt
实例009:暂停一秒输出
關於線性穩壓器的五個設計細節
Shape template matching based on Halcon learning [viii] PM_ multiple_ models. Hdev routine
Stm32--- systick timer
My-basic application 1: introduction to my-basic parser
Step motor generates S-curve upper computer
【三层架构】
Explication de la procédure stockée pour SQL Server
C WinForm [get file path -- traverse folder pictures] - practical exercise 6
实例001:数字组合 有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少?
The firmware of the connected j-link does not support the following memory access
Classic application of MOS transistor circuit design (2) - switch circuit design
Shape template matching based on Halcon learning [9] PM_ multiple_ dxf_ models. Hdev routine -- [read and write XLD from DXF file]
C language # and #
[trio basic from introduction to mastery tutorial XIV] trio realizes unit axis multi-color code capture
[paper reading] the latest transfer ability in deep learning: a survey in 2022
实例007:copy 将一个列表的数据复制到另一个列表中。
What are the test items of power battery ul2580
Why is 1900 not a leap year