题解 | 坐标移动
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
package main import ( "bufio" "fmt" "os" "strconv" "strings" ) var scanner = bufio.NewScanner(os.Stdin) type Point1 struct { X int Y int } func (p *Point1) MoveLeft(step int) *Point1 { p.X -= step return p } func (p *Point1) MoveRight(step int) *Point1 { p.X += step return p } func (p *Point1) MoveUp(step int) *Point1 { p.Y += step return p } func (p *Point1) MoveDown(step int) *Point1 { p.Y -= step return p } func (p *Point1) Move(command string) *Point1 { if len(command) < 2 { return p } step, _ := strconv.Atoi(command[1:]) switch command[:1] { case "A": p.MoveLeft(step) case "D": p.MoveRight(step) case "W": p.MoveUp(step) case "S": p.MoveDown(step) } return p } func main() { scanner.Scan() commands := strings.Split(strings.Trim(scanner.Text(), ";"), ";") p := &Point1{0, 0} for _, command := range commands { p.Move(command) } fmt.Printf("%d,%d", p.X, p.Y) }#坐标移动#