java.李雷和韩梅梅的加密纸条

题目:
李雷和韩梅梅坐前后排,上课想说话怕被老师发现,所以改为传小纸条。为了不被老师发现他们纸条上说的是啥,他们约定了如下方法传递信息:将26个英文字母(全为大写),外加空格,一共27个字符分成3组,每组9个。也就是ABCDEFGHI是第一组,JKLMNOPQR是第二组,STUVWXYZ是第三组(此处用代表空格)。
然后根据传递纸条那天的日期,改变字母的位置。先根据月份数m,以整个分组为单位进行循环左移,移动(m-1)次。然后根据日期数d,对每个分组内的字符进行循环左移,移动(d-1)次。以3月8日为例,首先移动分组,3月需要循环左移2次,变成:STUVWXYZ*,ABCDEFGHI,JKLMNOPQR。然后每组内的字符,8日的话需要循环左移7次,最终的编码为:Z*STUVWXY,HIABCDEFG,QRJKLMNOP。
对于要传递信息中的每个字符,用组号和组内序号两个数字来表示。如果在3月8日传递信息“HAPPY”,那么H位于第2组的第1个,A位于第2组第3个,P位于第3组第9个,Y位于第1组第9个,所以纸条上会写成:21 23 39 39 19。
现在给定日期和需要传递的信息,请输出应该写在纸条上的编码。

输入规范:
每个输入包含两行。第一行是用空格分隔的两个数字,第一个数字是月份,第二个数字是日子。输入保证是一个合法的日期。
第二行为需要编码的信息字符串,仅由A~Z和空格组成,长度不超过1024个字符。
输出规范:
对每个输入,打印对应的编码,数字之间用空格分隔,每个输出占一行。

输入示例1:
1 1
HI
输出示例1:
18 19

输入示例2:
3 8
HAPPY
输出示例2:
21 23 39 39 19

输入示例3:
2 14
I LOVE YOU
输出示例3:
35 25 18 12 29 31 25 23 12 28

源码:

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Test3 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextLine()) {
            String date = in.nextLine();
            String word = in.nextLine();
            String res = helper(date, word);
            System.out.println(res);
        }
    }
    private static String helper(String date, String word) {
        String[] res = null;
        StringBuilder sb = new StringBuilder();
        String[] str1 = date.split(" ");
        int month = Integer.parseInt(str1[0]);
        int day = Integer.parseInt(str1[1]);
        month = (month - 1) % 3;
        if (month == 1) {
            res = new String[]{"JKLMNOPQR", "STUVWXYZ*", "ABCDEFGHI"};
        } else if (month == 2) {
            res = new String[]{"STUVWXYZ*", "ABCDEFGHI", "JKLMNOPQR"};
        } else {
            res = new String[]{"ABCDEFGHI", "JKLMNOPQR", "STUVWXYZ*"};
        }
        change(res, day);
        Map map = table(res);
        int pos = 0;
        for (int i = 0; i < word.length(); i++) {
            pos = map.get(word.charAt(i));
            sb.append(pos + " ");
        }
        return sb.toString();
    }

    private static Map table(String[] str) {
        Map map = new HashMap<>();
        char[] chars = null;
        String s = null;
        for (int i = 1; i < 4; i++) {
            s = str[i - 1];
            chars = s.toCharArray();
            for (int j = 0; j < chars.length; j++) {
                if (chars[j] == '*') {
                    map.put(' ', (i * 10 + j + 1));
                    continue;
                }
                map.put(chars[j], (i * 10 + j + 1));
            }
        }
        return map;
    }
    private static void change(String[] str, int day) {
        int time = (day - 1) % 9;
        int i = 0;
        String s = null;
        String tmp = null;
        String tmp2 = null;
        while (i < 3) {
            s = str[i];
            tmp = s.substring(0, time);
            tmp2 = s.substring(time);
            str[i] = tmp2 + tmp;
            i++;
        }
    }
}

你可能感兴趣的:(java.李雷和韩梅梅的加密纸条)