首页 > 技术文章 > PTA(BasicLevel)-1009 说反话

justLittleStar 2019-04-11 23:12 原文

一 、问题描述

       原题描述

  给定一句英语,要求你编写程序,将句中所有单词的顺序颠倒输出。不同于字符串逆序,这里需要的是将字符串中的单词顺序翻转。

  输入样例:Hello World Here I Come

  输出样例:Come I Here World Hello

 

二、解题思路

   将输入的字符串分为若干单词,然后将单词逆序输出。

speech = input()
words = speech.split(" ")
words = words[::-1]

i = 0
length = len(words)
for w in words:
    i += 1
    print( w, end="")
    if i < length:
        print(" ", end="")

 

推荐阅读