题解 | #坐标移动#

坐标移动

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)
}


全部评论

相关推荐

小鹏、大疆、米哈游、MinMax小鹏上午投的下午就约面,进度未免也太快了
蛇年行大运fff:哥们 盗贴有意思吗,我发xhs上的给你搬过来了😅😅😅
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务