题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
package main //正则,switch语法,string切片技巧 import ( "fmt" "regexp" "strconv" "strings" ) func main() { var ( st string x int y int ) fmt.Scan(&st) split := strings.Split(st, ";") for _, buf := range split { result1, _ := regexp.MatchString(`[WASD][0-9]{1,2}`, buf) if result1 == true { head := buf[:1] num, _ := strconv.Atoi(buf[1:]) switch head { case "A": x -= num case "S": y -= num case "W": y += num case "D": x += num } } } fmt.Printf("%d,%d",x,y) }