题解 | #坐标移动#

坐标移动

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

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

struct Point {
    x: i32,
    y: i32
}

impl Point {
    fn move1(&mut self, cmd: &str) {
        if cmd.trim().len() == 0 {return}
        if !"WASD".contains(cmd.chars().nth(0).unwrap()) {return}
        if let Ok(d) = cmd[1..cmd.len()].trim().parse::<i32>() {
            match cmd.chars().nth(0).unwrap() {
                'W' => self.y += d,
                'A' => self.x -= d,
                'S' => self.y -= d,
                'D' => self.x += d,
                default => {}
            }
        }
    }

    fn moven(&mut self, cmds: &str) {
        for c in cmds.split(";") {
            self.move1(c);
        }
    }
}

fn main() {
    let stdin = io::stdin();
    unsafe {
        for line in stdin.lock().lines() {
            let ll = line.unwrap();
            
            let mut p = Point {x: 0, y: 0};
            p.moven(ll.trim());
            println!("{},{}", p.x, p.y)
        }
    }
}

全部评论

相关推荐

点赞 评论 收藏
分享
喜欢喜欢喜欢:这是我见过最长最臭的简历
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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