题解 | #句子逆序#
句子逆序
https://www.nowcoder.com/practice/48b3cb4e3c694d9da5526e6255bb73c3
golang版本
- go的需要注意不能用fmt.Scan函数去接收输入,因为空格会被认为是结束符
- 所以改用bufio.NewReader去接收
package main import ( "bufio" "fmt" "os" "strings" ) func main() { a := "" reader := bufio.NewReader(os.Stdin) a, err := reader.ReadString('\n') a = strings.TrimSpace(a) if err != nil { return } else { res := []string{} aSplit := strings.Split(a, " ") for i := len(aSplit) - 1; i > -1; i-- { res = append(res, aSplit[i]) } fmt.Println(strings.Join(res, " ")) } }
python版本
scan = input() scan_split = scan.split(' ') res = [] for i in range(len(scan_split) - 1, -1, -1): res.append(scan_split[i]) print(" ".join(res))