题解 | #字符串加解密#

字符串加解密

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

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNextLine()) {
            encodingsss(sc.nextLine());
//             for(int i = 0; i < raws.length; i++) {
//                 System.out.print(raws[i]);
//             }
            System.out.println();
            
//             char[] a = encodeStr.toCharArray();
            decoding(sc.nextLine());
//             for(int i = 0; i < a.length; i++) {
//                 System.out.print(a[i]);
//             }
            System.out.println();
        }
        
    }
    
    public static void encodingsss(String raws) {
        char[] raw = raws.toCharArray();
        for(int i = 0; i < raw.length; i++) {
            if(raw[i] >= 'a' && raw[i] < 'z') {
                raw[i] -= 31;
            }
            else if(raw[i] == 'z') {
                raw[i] = 'A';
            }
            else if(raw[i] >= 'A' && raw[i] < 'Z') {
                raw[i] += 33;
            }
            else if(raw[i] == 'Z') {
                raw[i] = 'a';
            }
            else if(raw[i] >= '0' && raw[i] < '9') {
                raw[i] += 1;
            }
            else if(raw[i] == '9') {
                raw[i] = '0';
            }
            System.out.print(raw[i]);
        }
    }
    
    public static void decoding(String raws) {
        char[] raw = raws.toCharArray();
        for(int i = 0; i < raw.length; i++) {
            if(raw[i] > 'a' && raw[i] <= 'z') {
                raw[i] -= 33;
            }
            else if(raw[i] == 'a') {
                raw[i] = 'Z';
            }
            else if(raw[i] >'A' && raw[i] <='Z') {
                raw[i] += 31;
            }
            else if(raw[i] == 'A') {
                raw[i] = 'z';
            }
            else if(raw[i] > '0' && raw[i] <= '9') {
                raw[i] -= 1;
            }
            else if(raw[i] == '0') {
                raw[i] = '9';
            }
            System.out.print(raw[i]);
        }
    }
}

// We still can use another method to increase the speed, like switch case etc, Thank you four your appreciation
全部评论

相关推荐

10-13 17:47
门头沟学院 Java
wulala.god:图一那个善我面过,老板网上找的题库面的
点赞 评论 收藏
分享
11-01 20:03
已编辑
门头沟学院 算法工程师
Amazarashi66:这种也是幸存者偏差了,拿不到这个价的才是大多数
点赞 评论 收藏
分享
点赞 2 评论
分享
牛客网
牛客企业服务