题解 | #字符串加解密#

字符串加解密

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

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        try(BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
            String str = br.readLine();
            char[] chars = str.toCharArray();
            Map<Character, Integer> map = new HashMap<>();
            for (int i = 0; i < 26; i++) {
                map.put((char)(i + 65), i + 65);
            }
            for (int i = 0; i < 26; i++) {
                map.put((char)(i + 97), i + 97);
            }
            for (int i = 0; i < 10; i++) {
                map.put((i + "").charAt(0), i);
            }
            StringBuffer sb = new StringBuffer();
            //加密
            for (char c : chars) {
                Integer index = map.get(c);
                if (index != null) {
                    if (index == (65 + 25)) {
                        index = 65;
                    } else if (index == 97 + 25) {
                        index = 97;
                    } else if (index == 9) {
                        index = 0;
                    } else {
                        index++;
                    }
                    if (index >= 0 && index <= 9) {
                        sb.append(index);
                    } else if (c >= 'a' && c <= 'z') {
                        sb.append((char)(index.intValue() - 32));
                    } else {
                        sb.append((char)(index.intValue() + 32));
                    }
                } else {
                    sb.append(c);
                }
            }
            System.out.println(sb.toString());
            //解密
            str = br.readLine();
            sb.setLength(0);
            chars = str.toCharArray();
            for (char c : chars) {
                Integer index = map.get(c);
                if (index != null) {
                    if (index == 65) {
                        index = 65 + 25;
                    } else if (index == 97) {
                        index = 97 + 25;
                    } else if (index == 0) {
                        index = 9;
                    } else {
                        index--;
                    }
                    if (index >= 0 && index <= 9) {
                        sb.append(index);
                    } else if (c >= 'a' && c <= 'z') {
                        sb.append((char)(index.intValue() - 32));
                    } else {
                        sb.append((char)(index.intValue() + 32));
                    }
                } else {
                    sb.append(c);
                }
            }
            System.out.println(sb.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


全部评论

相关推荐

面试官问:为什么不考研?该怎么回答啊😭我说现在的就业环境差到底了,还有就是我不想学数学,感觉面试官笑容都凝固了😢
DayDayNoBug的鲜芋球:我说的是“上学期其实尝试过去探索一些研究的方向,但感觉那些对我来说都没有很大的吸引力,相比起研究我可能更喜欢开发这种实践性的东西,它会让我觉得很有意思并且会为之深入进去”(虽然也不知这个回答怎么样哈哈哈哈哈哈)
点赞 评论 收藏
分享
04-29 00:12
小米_人力资源
牛客448863700号:也得看岗位呀,我还拿下美团呢,不说了送单了
点赞 评论 收藏
分享
见见123:简历没有啥问题,是这个社会有问题。因为你刚毕业,没有工作经历,现在企业都不要没有工作经历的。社会病了。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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