题解 | #输入整型数组和排序标识,对元素按照升序降序排序#
输入整型数组和排序标识,对其元素按照升序或降序进行排序
https://www.nowcoder.com/practice/dd0c6b26c9e541f5b935047ff4156309
while True:
try:
a = input().strip()
b = [int(x) for x in input().split()]
c = input().strip()
if len(b) > int(a): break
else:
if int(c) == 0:
print(" ".join(map(str,sorted(b))))
elif int(c) == 1:
print(" ".join(map(str,sorted(b)[::-1])))
else:
break
except:
break
tips:
不想这么写sorted(b)[::-1] ,可以考虑sorted(b,reverse=1) (0为默认正序,1为倒序)
华为机试(python3) 文章被收录于专栏
少壮不努力,老大勤刷题
查看16道真题和解析