题解 | #输入整型数组和排序标识,对其元素按照升序或降序进行排序#
输入整型数组和排序标识,对其元素按照升序或降序进行排序
http://www.nowcoder.com/practice/dd0c6b26c9e541f5b935047ff4156309
先用split将字符串以空格为标志间隔开,再用冒泡排序法将列表排序,排完序后再将列表转化为以空格隔开的字符串
n = int(input())
str_n = input().split()
k = int(input())
str_l = list(str_n)
for i in range(0,len(str_l)):
for j in range(i,len(str_l)):
if k == 0 :
if int(str_l[i]) > int(str_l[j]):
t = str_l[i]
str_l[i] = str_l[j]
str_l[j] = t
elif k == 1:
if int(str_l[i]) < int(str_l[j]):
t = str_l[i]
str_l[i] = str_l[j]
str_l[j] = t
print(' '.join(str_l))