HJ101 输入整型数组和排序标识,对其元素按照升序或降序进行排序
思路:
step1:首先,第一行输入数组元素个数,转换为整型;第二行输入数组,用list(map())转换为整型;第三行传入参数s,也转换为整型;
step2:如果 s==0 ,将num用sort()函数排序,默认正序排列;之后for循环,遍历排序后的num,输出打印,并用空格隔开;
step3:如果 s==1,将num用sort()函数排序,倒序排列(reverse = True);之后for循环,遍历排序后的num,输出打印,并用空格隔开
代码如下:
n = int(input())
num = list(map(int,input().split()))
s = int(input())
if s == 0:
num.sort()
for i in num:
print(i,end=' ')
elif s == 1:
num.sort(reverse = True)
for i in num:
print(i,end=' ')