a104: 排序
这篇教学会示范 ZeroJudge 基础题库“a104: 排序”的解题过程。
题目需求
题目会提供一串数字,透过程序将这些数字从小到大排序后输出。
题目链接:a104: 排序
解答
使用生成式将输入的文字转换成数字串列,再透过串列的 sort() 排序功能排序后输出。
while True:
try:
n = int(input()) # 有幾個數字
nums = [int(i) for i in input().split(' ')] # 使用生成式轉換成數字串列
nums.sort() # 排序
for i in range(n):
print(f'{nums[i]}', end=' ') # 輸出
print()
except:
break
微信扫码关注
抖音扫码关注