题解 | #坐标移动#

坐标移动

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

字符串切片真的太棒啦!

use std::io::{self, *};
use std::str::FromStr;

fn main() {
    let stdin = io::stdin();
    for line in stdin.lock().lines() {
        let (mut x,mut y) = (0i32,0i32);
        let ll = line.unwrap();
        let numbers: Vec<&str> = ll.split(";").collect();
        for i in 0..numbers.len(){
            let len = numbers.len();
            let mut nums = 0i32;
            for j in numbers[i].as_bytes() {
                if *j > 57u8 || *j < 48u8 {
                    nums += 1;
                }
            }
            let behav = match numbers[i].get(0..1) {
                Some("A") => 'A',
                Some("W") => 'W',
                Some("S") => 'S',
                Some("D") => 'D',
                _ => ' ',
            };

            if len > 1 && behav != ' ' && nums == 1 {
                let dist = match numbers[i].get(1..) {
                    Some(x) => i32::from_str(x).unwrap(),
                    None => 0i32,
                };
                if behav == 'A' {
                    x -= dist;
                }else if behav == 'D' {
                    x += dist;
                }else if behav == 'S' {
                    y -= dist;
                }else {
                    y += dist;
                }
            }
        }
            print!("{},{}\n",x,y);
    }
}
用 Rust 刷华为机试HJ 文章被收录于专栏

用 Rust 刷 HJ100 题,只需要懂基础 Rust 语法就能看懂

全部评论
该牛油正在参与牛客写题解薅羊毛的活动,牛币,周边,京东卡超多奖品放送,活动进入倒计时!快来捡漏啦https://www.nowcoder.com/discuss/888949?source_id=profile_create_nctrack&channel=-1
点赞 回复 分享
发布于 2022-04-20 17:25

相关推荐

不愿透露姓名的神秘牛友
07-10 11:45
你不要过来啊啊啊啊啊啊啊
码农索隆:对面:“今天你不面也得面”
点赞 评论 收藏
分享
06-05 19:46
已编辑
武汉大学 后端
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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