题解 | #坐标移动#

坐标移动

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

package main

import (
	"fmt"
	"strconv"
	"strings"
)

func main() {
	// 答案
	res := [2]int{0, 0}
	var in string
	fmt.Scan(&in)

	t := strings.Split(in, ";")
	for i := 0; i < len(t); i++ {
	  	//  按分号分隔后的每个单词
		str := t[i]
	  	// 提前判断不合格
		if len(str) < 2 {
			continue
		}
	  	// 方向字符
		d := str[0]
		if d == 'A' || d == 'S' || d == 'D' || d == 'W' {
		  	// 将位移量取出
			str = str[1:]
			num, _ := strconv.Atoi(str)
		  	// 进行移动
			switch d {
			case 'A':
				res[0] -= num
			case 'S':
				res[1] -= num
			case 'D':
				res[0] += num
			case 'W':
				res[1] += num
			}
		}
	}
  // 输出答案
	fmt.Printf("%d,%d\n", res[0], res[1])
}

全部评论

相关推荐

是每个人事都这样与找工作的人这样沟通吗?正常询问不可以吗
据说名字越长别人越关注你的昵称我觉得我要被关注了:excal 我还真不会
点赞 评论 收藏
分享
喜欢核冬天的哈基米很想上市:会爆NullPointerException的
点赞 评论 收藏
分享
05-29 22:11
门头沟学院 Java
Elastic90:抛开学历造假不谈,这公司的招聘需求也挺怪的,Java开发还要求你有图文识别、移动端开发和c++的经验,有点逆天了。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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