a915: 二维点排序
这篇教学会示范 ZeroJudge 基础题库“a915: 二维点排序”的解题过程。
题目需求
题目会提供一连串的“两个数字”串列,透过程序将这些串列由小到大排序 (先排第一个数,如果第一个数相同,就以第二个数进行排序 )。
题目链接:a915: 二维点排序
解答
先将题目所提供的两个数字存入串列,再透过 sorted 方法进行排序,就能得到最后的结果。
while True:
try:
n = int(input()) # 取得接下來有幾個數
arr = [] # 建立排序用的串列
for i in range(n):
p, q = map(int, input().split())
arr.append([p, q]) # 陸續將題目提供的兩個數字,轉換成串列存入 arr
output = sorted(arr) # output 為 arr 由小到大排序
for i in output:
print(*i) # 輸出 output 內容
except:
break
微信扫码关注
抖音扫码关注