题解 | #字符串加解密#

字符串加解密

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);
    }
}

全部评论

相关推荐

06-14 19:09
门头沟学院 Java
darius_:给制造业搞的,什么物料管理生产管理,设备管理点检,最最关键的就是一堆报表看板。个人觉得没啥技术含量都是些基本的crud,但是业务很繁琐那种
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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