目录
1. 输入正整数,字符串长度在要求范围内,字符串全部由字母构成
题目描述
给定n个字符串,请对n个字符串按照字典序排列。输入第一行为一个正整数n(1≤n≤1000),下面n行为n个字符串(字符串长度≤100),字符串中只含有大小写字母。数据输出n行,输出结果为按照字典序排列的字符串。
例如:
输入:
9
cap
to
cat
card
two
too
up
boat
boot
输出
boat
boot
cap
card
cat
to
too
two
up
解决方案
1、接收输入的整数,表示接下来输入的字符串数量
2、接收若干个字符串
3、按照字典序列排序
4、输出排序结果
代码
if __name__ == "__main__":
try:
amount = int(input())
if amount < 1 or amount > 1000:
raise Exception()
tem_list = []
for i in range(amount):
tem_string = input()
if len(tem_string) > 100:
raise Exception
if not tem_string.isalpha():
raise Exception
tem_list.append(tem_string)
tem_list.sort()
for element in tem_list:
print(element)
except Exception:
exit()
代码走读
if __name__ == "__main__":
try:
# 接收一个整数,表示接下来需要排序的字符串数量
amount = int(input())
# 题目要求整数的范围在1 ~ 1000,因此对超出范围的整数抛出异常
if amount < 1 or amount > 1000:
raise Exception()