题解 | #坐标移动#正则方法

坐标移动

https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29

package main

import (
	"bufio"
	"fmt"
	"os"
	"regexp"
	"strconv"
	"strings"
)

func main() {
    // 接收书入字符串并划分成数组
    scan:=bufio.NewScanner(os.Stdin)
    scan.Scan()
    inputarr:=strings.Split(scan.Text(), ";")
    // fmt.Println(inputarr[len(inputarr)-1]=="")
    // 创建x,y两个坐标
    x,y:=0,0
    // 对数组进行处理
    for _,v:=range inputarr{
        // 值为空字符的话直接跳过
        if v==""{
            continue
        }
        //首先判断数组是否符合规则,我这里使用的是正则
        if flag, _ := regexp.MatchString(`^(A|W|S|D)[1-99]`, v);flag{
            // 当正则通过再进行坐标处理
            // 把对应的数字拿出来
            res,_:=strconv.Atoi(v[1:])
            switch v[0]{
                case 'A':
                x-=res
                case 'D':
                x+=res
                case 'W':
                y+=res
                case 'S':
                y-=res
            }
        }
    }
    fmt.Printf("%d,%d",x,y) 
}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务