题解 | #字符串排序#
字符串排序
https://www.nowcoder.com/practice/5af18ba2eb45443aa91a11e848aa6723?tpId=37&tags=&title=&difficulty=0&judgeStatus=0&rp=1&sourceUrl=%2Fexam%2Foj%2Fta%3FtpId%3D37
#递归法比较两字符串大小
#如果str1小则return True 否则 return False
def compare_str(str1,str2):
if str1=='':
return True
if str2=='':
return False
elif ord(str1[0])<ord(str2[0]):
return True
elif ord(str1[0])>ord(str2[0]):
return False
else:
return compare_str(str1[1:],str2[1:])
N=n=int(input())
ls_str=[]
while n>0:
ls_str.append(input())
n-=1
#比较字符串大小 冒泡排序
for item in range(0,N):
for itemj in range(item+1,N):
if compare_str(ls_str[item],ls_str[itemj])==False:
ls_str[itemj],ls_str[item]=ls_str[item], ls_str[itemj]
#输出
for i in range(0,N):
print(ls_str[i])
#递归yyds#