题解 | #字符串加解密#

字符串加解密

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

相关推荐

2025-12-14 11:43
黑龙江大学 Java
用微笑面对困难:确实比较烂,可以这么修改:加上大学的qs排名,然后大学简介要写一些,然后硕士大学加大加粗,科研经历第一句话都写上在复旦大学时,主要负责xxxx,简历左上角把学校logo写上,建议用复旦大学的简历模板
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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