题解 | #坐标移动#

坐标移动

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

pub fn caculate_location() -> (i32, i32) {
    let mut input = String::new();
    std::io::stdin()
        .read_line(&mut input)
        .expect("Failed to read line");
    input
        .trim()
        .split(";")
        .map(|item| {
            if item.len() < 2 {
                (0, 0)
            } else {
                let (s1, s2) = item.split_at(1);
                if let Ok(value) = s2.parse::<i32>() {
                    if s1 == "A" {
                        (-value, 0)
                    } else if s1 == "D" {
                        (value, 0)
                    } else if s1 == "W" {
                        (0, value)
                    } else if s1 == "S" {
                        (0, -value)
                    } else {
                        (0, 0)
                    }
                } else {
                    (0, 0)
                }
            }
        })
        .fold((0, 0), |acc, x| (acc.0 + x.0, acc.1 + x.1))
}

fn main() {
    let v=caculate_location();
    println!("{},{}",v.0,v.1);
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
06-21 11:29
凉风落木楚山秋:他们两都收获了流量,只有爷浪费了时间
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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