题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
package main import ( "fmt" "runtime" "strconv" "strings" ) func main() { var input string var ret [2]int _, err := fmt.Scan(&input) if err != nil { runtime.Goexit() return } inArr := strings.Split(input, ";") if len(inArr) == 0 { // fmt.Println(ret) return } match := func(str string) bool { if len(str) < 2 || len(str) > 3 { return false } if str[0] != 'W' && str[0] != 'S' && str[0] != 'D' && str[0] != 'A' { return false } if str[1] < '0' || str[1] > '9' { return false } if len(str) == 2 { return true } if str[2] < '0' || str[2] > '9' { return false } return true } // reg := regexp.MustCompile("^A|D|W|S[0-9]{1,2}$") for _, v := range inArr { // if !reg.MatchString(v) { if !match(v) { // fmt.Println("continue") continue } value, err := strconv.Atoi(v[1:]) if err != nil { continue } switch v[0] { case 'A': ret[0] -= value case 'D': ret[0] += value case 'W': ret[1] += value case 'S': ret[1] -= value } } fmt.Printf("%d,%d\n", ret[0], ret[1]) }