题解 | #字符串加密#

字符串加密

https://www.nowcoder.com/practice/e4af1fe682b54459b2a211df91a91cf3

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别

        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String str = in.nextLine();
            String res = in.nextLine();
		  //存放新顺序的26个字母
            char [] arr  = new char[26];
		  //检验字母重复
            int [] check = new int [26];
            int j = 0;
		  //将key去重放入 arr中
            for (int i = 0 ; i < str.length(); i++) {
                char c = str.charAt(i);
                if (check[c - 'a'] == 0) {
                    arr[j] = c;
                    j++;
                }
                check[c - 'a'] = 1;
            }
		//将剩余的字母按顺序放入arr中
            for(int i = 0 ; i <26 ; i++){
                if(check[i]==0){
                    arr[j] = (char)('a'+i);
                    j++;
                }
            }
            StringBuffer resBuffer = new StringBuffer();
		  //解密
            for(int i = 0 ; i <res.length();i++){
              char c =   arr[res.charAt(i)-'a'];
              resBuffer.append(c);

            }
            System.out.println(resBuffer);
        }
    }
}

全部评论

相关推荐

点赞 评论 收藏
分享
2024-12-27 10:21
已编辑
海南师范大学 媒介策划
到我怀里来:身高体重住址这些就别写了,留几个关键的就行,工作经历突出重点写详细点
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务