笔者先是这么写的,但是只有12分,后续检测点全部报错。
import java.io.*;
public class pat1044 {
public static String[] number = {"tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};
public static String[] uppernum = {"tret", "tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(input.readLine());
String[] arr = new String[N];
for(int i=0;i<N;i++) arr[i] = input.readLine();
input.close();
for(int i=0;i<N;i++) {
String str = arr[i]; //通过判断第一个char是不是数字,来判断是 地转火,还是火转地
if(str.charAt(0) - '0' < 10 && str.charAt(0) - '0' >= 0) toMars(str);
else toEarth(str);
}
}
public static void toMars(String a) {
String num = Integer.toString(Integer.parseInt(a), 13); //转换成 13 进制的数字 10-->a,11-->b,12-->c,13-->10
for(int i=0;i<num.length()-1;i++) { //然后用 ASCII 识别并且输出
if(num.charAt(i) > 96) System.out.print(uppernum[num.charAt(i) - 87] + " "); //转换成 数组位置
else System.out.print(uppernum[num.charAt(i) - '0'] + " ");
}
if(num.charAt(num.length()-1) > 96) System.out.print(uppernum[num.charAt(num.length()-1) - 87] + " ");
else System.out.print(number[num.charAt(num.length()-1) - '0'] + "\n");
}
public static void toEarth(String a) {
String[] str = a.split(" ");
int num = 0;
for(int i=0;i<str.length-1;i++) {
for(int j=0;j<13;j++) {
if(str[i].equals(uppernum[j])) {
num = (num+j)*13;
break;
}
}
}
if(num==0 && str[str.length-1].equals(uppernum[1])) num += 13;
else {
for(int i=0;i<13;i++) if(str[str.length-1].equals(number[i])) {
num += i;
break;
}
}
System.out.println(num);
}
}
在学习了其他人的算法以后,发现扣分点主要在于
1.我没有使用 除 和 余的方法计算“地转火”,而是直接使用了方法Integer.toString(num, 13); 这个似乎不被pat接受。(但是前面的PAT1022接受了这个方法,我呸)
2.使用的是相加,因为tam在uppernum中不会被 number[].equals() 识别到,结果为0,所以我把数字13,也就是只输出 tam 的情况单独列出了。做了一个 if(num==0 && str.charAt().equals("tam")) 的判断,似乎也是扣分点。
下面是改过以后20分的代码
说实话我个人是不服的,我第一轮的代码在功能性和完整性上并没有出错,有意的写了可以转换任何 int 值内的正整数的功能,却扣了分,害!
import java.io.*;
public class pat1044 {
public static String[] number = {"tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};
public static String[] uppernum = {"tret", "tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(input.readLine());
String[] arr = new String[N];
for(int i=0;i<N;i++) arr[i] = input.readLine();
input.close();
for(int i=0;i<N;i++) {
char chara = arr[i].charAt(0); //通过判断第一个char是不是数字,来判断是 地转火,还是火转地
if(chara>47 && chara<58) toMars(arr[i]);
else toEarth(arr[i]);
}
}
public static void toMars(String str) {
int num = Integer.parseInt(str);
if(num==0) System.out.print(number[0]); //如果数字是0,只输出 tret
else {
int a = num/13; //十位数
int b = num%13; //个位数
if(a==0) System.out.print(number[b]); //如果没有十位数,就只输出个位数
else {
System.out.print(uppernum[a]);
if(b!=0) System.out.print(" " + number[b]); //输出个位数和空格
}
}
System.out.println();
}
public static void toEarth(String str) {
String[] arr = str.split(" ");
int num = 0;
if(arr.length == 1) { //如果只有一位数
if(find(number, arr[0]) > -1) num = find(number, arr[0]); //如果第一个值是number 而不是uppernum
else num = find(uppernum, arr[0]) * 13; //输出13
}else num = find(uppernum, arr[0]) * 13 + find(number, arr[1]); //输出两位数
System.out.println(num);
}
public static int find(String[] arr, String num) {
for(int i=0;i<arr.length;i++) {
if(num.equals(arr[i])) {
return i;
}
}
return -1; //没有的话返回一个越界值,也就是数组没有的位置
}
}