题解 | #句子逆序#
句子逆序
http://www.nowcoder.com/practice/48b3cb4e3c694d9da5526e6255bb73c3
#注意本题有多组输入
while True:
try:
n = input()
#print(n)
list_str =n.split(' ')
#print(list_str)
list_str.reverse()
print(' '.join(list_str))
except:
break
经常用split,都不会写循环了,这个破玩意写了俩小时。感觉写复杂了。
while True:
try:
in_str = input()
out_str=''
word=''
#print(n)
for i in in_str:
if i !=' ':
# 当i不为空的时候,会从头到尾迭代,此时应该顺序拼接
word = word+i
#print(word)
else:
# 此时i为空,需要把空格进行拼接,word清空
out_str= i+word+out_str
word = ''
# 循环外面查一次最后的word
print(word + out_str)
except:
break