粤澳程序设计赛A题:Alice的秘密(java)

Alice有一些重要信息,这信息只包括大写字母、小写字母和空格。她想要对这些信息进行记录,但是又不想直接记录原始信息。

她开始查找资料,其中一种方法是将字母进行变换,设定一个变换位置k,则c=k+m,其中m为原始字符,c为变换结果。比如k=2,则A变为C,Y变为A,z变为b。空格转换为’#’。

Alice对k值的设置方法进行了一些改进,由原来固定的k值改进为使用记录的日期变换出k值。具体方法是对日期构成的每一位求和,直到k小于10。比如20020308,对日期构成的每一位求和得到k=2+2+3+8=15,继续求和k=1+5=6,则最终得到k=6。请你帮Alice编写一个程序,利用记录日期将信息转换为加密编码格式。

这个算法要求日期的格式为:yyyymmdd,年份范围应为[1900-2020],月份范围应为[1-12],日根据年和月确定;信息只能是大写字母、小写字母和空格。

输入要求
多组输入。

每组数据包括2行。第1行是一个日期,长度不大于8;第2行是要加密的信息,长度不大于128。

输出要求
每组数据输出如下:

如果输入的日期或信息不符合要求,输出 none

否则,输出加密后的信息。

示例:
输入
20201206
I wrote a program to get a grade A
1234
GMCPC

输出
M#avsxi#e#tvskveq#xs#kix#e#kvehi#E
none

(个人解法,欢迎指正)

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class A {
    static Boolean judge(String s) {
        if (s.length() != 8) return false;
        int y = Integer.parseInt(s.substring(0, 4));
        int m = Integer.parseInt(s.substring(4, 6));
        int d = Integer.parseInt(s.substring(6, 8));
        if (y < 1900 || y > 2020) return false;
        if (m < 1 || m > 12) return false;

        int[] D = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        if (d <= 0 || d > D[m]) return false;
        return true;
    }

    static int toInt(String s) {
        String[] split = s.split("");
        int[] a = new int[split.length];
        for (int i = 0; i < split.length; i++) {
            a[i] = Integer.parseInt(split[i]);
        }
        int count = 0;
        for (int j = 0; j < a.length; j++) {
            count += a[j];
        }
        if (count >= 10) {
            String newS = count + "";
            count = toInt(newS);
        }
        return count;
    }

    static String alice(char[] wordArray, int n) {
        for (int i = 0; i < wordArray.length; i++) {
            if (wordArray[i] == 32) {
                wordArray[i] = 35;
                continue;
            }
            if (wordArray[i] >= 65 && wordArray[i] + n <= 90) {
                wordArray[i] += n;
                continue;
            }
            if (wordArray[i] + n > 90 && wordArray[i] + n < 99) {
                wordArray[i] = (char) (wordArray[i] + n - 26);
                continue;
            }
            if (wordArray[i] >= 97 && wordArray[i] + n <= 122) {
                wordArray[i] += n;
                continue;
            }
            if (wordArray[i] + n > 122) {
                wordArray[i] = (char) (wordArray[i] + n - 26);
                continue;
            }
        }
        String s = String.valueOf(wordArray);
        return s;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        List<String> s = new ArrayList<>();
        List<String> word = new ArrayList<>();
        String s1 = "";
        String s2 = "";
        while (!(s1=sc.nextLine().trim()).equals("") && !(s2=sc.nextLine()).equals("")) {
            s.add(s1);
            word.add(s2);
        }
        for (int i = 0; i < s.size(); i++) {
            if (!judge(s.get(i))) {
                System.out.println("none");
            } else {
                char[] wordArray = word.get(i).toCharArray();
                int n = toInt(s.get(i));
                System.out.println(alice(wordArray, n));
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_44260464/article/details/105797871