题解 | #字符串加解密#

字符串加解密

http://www.nowcoder.com/practice/2aa32b378a024755a3f251e75cbf233a

工作技能可以写熟悉ascii码的背诵么?(笑)

alt

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

fn encry(s:Vec<u8>) -> String {
    let mut st = String::new();
    for i in s {
        let c = match i {
            48..=56 => i+1,//0 8 -> 1 9
            57 => 48,// 9 -> 0
            65..=89 => i + 33,//A Y -> b z
            90 => 97,//Z -> a
            97..=121 => i - 31,//a y -> B Z
            122 => 65,//z -> A
            _ => i,
        };
        st.push(char::from(c));
    }
    st
}

fn decry(s:Vec<u8>) -> String {
    let mut st = String::new();
    for i in s {
        let c = match i {
            48 => 57,//0 => 9
            49..=57 => i-1,//1 9 -> 0 8
            65 => 122,//A -> z
            66..=90 => i + 31,//B Z -> a y
            97 => 90,//a -> Z
            98..=122 => i - 33,//b z -> A
            _ => i,
        };
        st.push(char::from(c));
    }
    st
}

fn main() {
    let stdin = io::stdin();
    let mut is_first = true;
    for line in stdin.lock().lines() {
        let ll = line.unwrap();
        let s: Vec<u8> = Vec::from(ll);
        if is_first {
            println!("{}",encry(s));
            is_first = false;
        }else{
            println!("{}",decry(s));
            is_first = true;
        }
    }
}
用 Rust 刷华为机试HJ 文章被收录于专栏

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

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

相关推荐

12-13 14:51
已编辑
井冈山大学 算法工程师
龙虾x:算法比你强的没有你美,比你美的…..算了已经没有比你美的了
工作两年想退休了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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