题解 | 坐标移动
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
func getInput() []string {
var inputText string
reader := bufio.NewReader(os.Stdin)
fmt.Fscan(reader, &inputText)
return strings.Split(inputText, ";")
}
func main() {
commandSlice := getInput()
// 预设初始坐标0,0
x, y := 0, 0
for _, command := range commandSlice {
if len(command) > 1 {
if command[0] == 'A' || command[0] == 'W' || command[0] == 'S' || command[0] == 'D' {
temp := command[1:]
if number, err := strconv.Atoi(temp); err == nil && number < 100 && number > 0 && len(temp) < 3 {
switch command[0] {
case 'A':
x-=number
case 'D':
x+=number
case 'W':
y+=number
case 'S':
y-=number
}
}
// 字符串转int失败的不要
}
// 首字母不属于AWSD的不要
}
// 长度小于2的不要
}
fmt.Printf("%d,%d", x, y)
}
叮咚买菜工作强度 163人发布