首页 > 试题广场 >

句子逆序

[编程题]句子逆序
  • 热度指数:534559 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
\hspace{15pt}对于给定的若干个单词组成的句子,每个单词均由大小写字母混合构成,单词间使用单个空格分隔。输出以单词为单位逆序排放的结果,即仅逆序单词间的相对顺序,不改变单词内部的字母顺序。

输入描述:
\hspace{15pt}在一行上输入若干个字符串,每个字符串代表一个单词,组成给定的句子。
\hspace{15pt}除此之外,保证每个单词非空,由大小写字母混合构成,且总字符长度不超过 10^3


输出描述:
\hspace{15pt}在一行上输出一个句子,代表以单词为单位逆序排放的结果。
示例1

输入

Nowcoder Hello

输出

Hello Nowcoder
示例2

输入

A b C d

输出

d C b A
string = input()
words = string.split()
result = []
for i in words[::-1]:
    result.append(i)
print(" ".join(str(i) for i in result))
先根据拆出单词,再倒序一次存入list。最后按要求导出string
发表于 2021-06-07 14:18:23 回复(2)
line = input().strip()

words = []

for word in line.split(' '):
    words.insert(0, word)

print(' '.join(words))

编辑于 2021-06-03 17:53:31 回复(1)
def cal(s):
    curr = ""
    output = ""
    for i in s:
        if i == " ":
            output = i + curr + output
            curr = ""
        else:
            curr += i
    print(curr+output)
while True:
    try:
        cal(input())
    except:
        break
发表于 2021-04-20 17:25:05 回复(0)
while True:
    try:
        string = list(input().split())
        new = ''
        for i in range(1, len(string)):
            new = new+string[-i]+' '
        new += string[0]
        print(new)
    except:
        break
发表于 2021-03-29 17:23:53 回复(0)
python 实现
in_str = input()
array = in_str.split(' ')
for i in array:
    if not i.isalpha():
        raise("出现非英文字符")
array.reverse()
print(' '.join(array))

发表于 2021-03-27 00:47:23 回复(0)
while True:
    try:
        a = input().split(' ')
        output = ' '.join(reversed(a))
        print(output)
    except EOFError:
        break

编辑于 2021-03-24 13:39:50 回复(0)
###Code_1###
print(" ".join((input().split(" "))[::-1]))


###Code_2###
s=input()
h=s.split(" ")
t=h[::-1]
print(" ".join(t))

发表于 2021-02-21 03:20:28 回复(0)
a=input()
res=""
for i in list(reversed(a.split(" "))):
    res = res + i + " "
print(res.rstrip())

发表于 2021-02-03 17:39:36 回复(0)
python3 一行解决
print(" ".join(input().strip().split()[::-1]))


发表于 2021-01-31 19:43:46 回复(0)
def main():
    word_str = input()
    word_str_split = word_str.split(" ")    
    print(" ".join(word_str_split[::-1]))


if __name__ == "__main__":
    main()

发表于 2021-01-08 11:10:32 回复(0)
print(' '.join(reversed(input().split())))
发表于 2021-01-04 23:03:52 回复(0)
s=input().split(' ')
print(' '.join(s[::-1]))
虽然看那些写C++、C、Java的大佬我都很佩服,但是还是禁不住想笑

发表于 2020-12-26 23:05:46 回复(0)
print(" ".join(reversed(input().split())))

发表于 2020-12-13 21:44:51 回复(0)
A=input().split()
B=''
n=len(A)
i=1
while(i<=n):
    B+=(A[n-i]+' ')
    i+=1
print(B)
其实我觉得输出的尾部会多一个空格,但是测过了
发表于 2020-12-04 14:38:51 回复(0)
while True:
    try:
        str=input().split()
        str=str[::-1]
        print(' '.join(str))
    except:
        break
发表于 2020-11-30 16:20:16 回复(0)

words = input().split()
words_rev = words[::-1]
a = " ".join(words_rev)
print(a)

发表于 2020-11-29 15:15:54 回复(0)
str = input().split()
a = []
for i in range(len(str),0,-1):
    a.append(str[i-1])
print(" ".join(a))
发表于 2020-09-07 10:29:16 回复(0)
while True:
    try:
        a = input().split()
        a.reverse()
        print(' '.join(a))
    except:
        break
发表于 2020-08-24 16:36:46 回复(0)
逆序for循环
a = input().split(' ')
for i in range(len(a)-1, -1, -1):
    print(a[i], end=' ')

发表于 2020-08-21 11:49:43 回复(0)
line = input()
line1 = line.split()
line2 = " ".join(line1[::-1])
print(line2)
也可以用一句搞定,就···就这样吧
发表于 2020-08-18 11:46:04 回复(0)