题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
package main
import (
"fmt"
"strconv"
"strings"
)
func main() {
// 答案
res := [2]int{0, 0}
var in string
fmt.Scan(&in)
t := strings.Split(in, ";")
for i := 0; i < len(t); i++ {
// 按分号分隔后的每个单词
str := t[i]
// 提前判断不合格
if len(str) < 2 {
continue
}
// 方向字符
d := str[0]
if d == 'A' || d == 'S' || d == 'D' || d == 'W' {
// 将位移量取出
str = str[1:]
num, _ := strconv.Atoi(str)
// 进行移动
switch d {
case 'A':
res[0] -= num
case 'S':
res[1] -= num
case 'D':
res[0] += num
case 'W':
res[1] += num
}
}
}
// 输出答案
fmt.Printf("%d,%d\n", res[0], res[1])
}
