首页 > 试题广场 >

字符串排序(1)

[编程题]字符串排序(1)
  • 热度指数:43994 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
对输入的字符串进行排序后输出
打开以下链接可以查看正确的代码
https://ac.nowcoder.com/acm/contest/5657#question



输入描述:
输入有两行,第一行n

第二行是n个字符串,字符串之间用空格隔开


输出描述:
输出一行排序后的字符串,空格隔开,无结尾空格
示例1

输入

5
c d a bb e

输出

a bb c d e
推荐
点击链接查看正确的代码https://ac.nowcoder.com/acm/contest/5657#question
编辑于 2020-12-29 17:33:39 回复(0)
import sys

ans = ""
n = 0

for line in sys.stdin:
    a = line.split()
    if len(a) == 1 and (not n):
        n = len(a)
        continue
    a = sorted(a)
    for char in a:
        ans += char + ' '
    print(ans[:-1])
发表于 2023-09-04 20:14:49 回复(0)
import sys
# 读入第一行
num = int(input())
# 定义一个空字符串
Str = ""
# 读入第二行
for line in sys.stdin:
    List = list(map(str, line.split()))
    # 使用sort()方法对字符串进行排序
    List.sort()
    for i in range(len(List)):
        shortStr = str(List[i])
        Str += shortStr
        if i < num - 1:
            Str += ' '
    print(Str)

发表于 2022-01-15 10:15:19 回复(0)

热门推荐

通过挑战的用户