题解 | 凯撒解密

凯撒解密

https://www.nowcoder.com/practice/42c2f0c1351e4a6689ff64eddaf97a37

题干解读:要求将输入的字符串中的字符按字母表向前移动n位

解题思路:先写一个移动字符的函数decord,在该函数内对n向26取模,得到t,t为等效移动长度.

移动到ASCLL表的非字符部分,先进行处理,等效到字符表中,

最后遍历字符串,调用移动字符的函数decord即可.

class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 进行凯撒解密
     * @param password string字符串 旺仔哥哥的密码
     * @param n int整型 每个字符加密过程中错位的次数
     * @return string字符串
     */
    string decodeWangzai(string password, int n) {
        for(int i=0;i<password.length();i++){
            password[i] = decode(password[i], n);
        }
        return password;
        // write code here
    }
    char decode(char c,int n){
        int t=n%26;
        if(c-t<'a'){
            return 'z'- (t-1 - (c-'a'));
        }else{
            return c-t;
        }
    }
};

全部评论

相关推荐

找工作勤劳小蜜蜂:矛盾是没有实习,就是没实战经验,公司不想要,公司不要,你就没有实习,你就进入死循环,另外你的项目不是社会现在有大量岗位存在行业用的,云存储人员早就饱和。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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