题解 | 凯撒加密

凯撒加密

https://www.nowcoder.com/practice/006b7917d3784371a43cfbae01a9313d

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        in.nextLine();
        String s = in.nextLine();
        StringBuilder str = new StringBuilder(s);
        for(int i = 0;i < s.length(); i++){
            char a = str.charAt(i);
            str.setCharAt(i,fix(n,a));
        }
        String result = str.toString();
        System.out.print(result);


    }

    public static char fix(int n, char c){
        int c_ascii = (int) c;
        if(c_ascii + (n%26) <=122){
            return (char)(c_ascii + (n%26));
        }else{
            return (char)(c_ascii + (n % 26) - 26);
        }
    }
}

全部评论

相关推荐

痛痛痛痛信灬:我小米都面完两个月了 八月底面完的,现在还是显示面试中,没有比我恐怖的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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