题解 | #字符串加解密#

字符串加解密

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

循环除法:(value + radix) % radix

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println(convert(in.nextLine(), 1));
        System.out.println(convert(in.nextLine(), -1));
    }

    private static StringBuilder convert(String str, int type) {
        StringBuilder sb = new StringBuilder();
        for (char c : str.toCharArray()) {
            if (Character.isUpperCase(c)) {
                sb.append(convert(c, 'A', 'a', 26, type));
            } else if (Character.isLowerCase(c)) {
                sb.append(convert(c, 'a', 'A', 26, type));
            } else if (Character.isDigit(c)) {
                sb.append(convert(c, '0', '0', 10, type));
            }
        }
        return sb;
    }

    private static char convert(char c, char before, char after, int radix, int type) {
        return (char) ((c - before + type + radix) % radix + after);
    }
}

全部评论

相关推荐

牛客50327486...:腾讯官方:我们没有人机对局
点赞 评论 收藏
分享
09-17 19:25
已编辑
太原理工大学 游戏测试
叁六玖:公司名发我,我要这个HR带我打瓦
我的秋招日记
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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