题目描述:
火星人是以 13 进制计数的:
- 地球人的 0 被火星人称为 tret。
- 地球人数字 1 到 12 的火星文分别为:jan, feb, mar, apr, may, jun, jly, aug, sep, oct, nov, dec。
- 火星人将进位以后的 12 个高位数字分别称为:tam, hel, maa, huh, tou, kes, hei, elo, syy, lok, mer, jou。
例如地球人的数字 29 翻译成火星文就是 hel mar;而火星文 elo nov 对应地球数字 115。为了方便交流,请你编写程序实现地球和火星数字之间的互译。
-
输入格式:
输入第一行给出一个正整数 N(<100),随后 N 行,每行给出一个 [0, 169) 区间内的数字 —— 或者是地球文,或者是火星文。 -
输出格式:
对应输入的每一行,在一行中输出翻译后的另一种语言的数字。
解题思路:首先判断输入的字符串是地球数字还是火星数字,如果是地球数字,则将其转化为火星文,直接取余取整操作后取对应数组对应索引位置的火星文即可,相对较简单。若是火星文,将其通过空格分割,若长度大于1,通过getIndex函数取得两个数组中相应的索引值计算出对应地球的数字,若长度为1,则只需取一个数组中的索引计算即可
易错点:当输入13,26等13的倍数的时候要特别注意,七对应的火星文也是只有一位。所以需要另加判断。
程序:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main{
public static void main(String[] args) throws IOException{
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(bf.readLine());
String[] s1 = {"jan","feb","mar","apr","may","jun","jly","aug","sep","oct","nov","dec"};
String[] s2 = {"tam","hel","maa","huh","tou","kes","hei","elo","syy","lok","mer","jou"};
for (int i = 0; i < n; i++) {
String s = bf.readLine();
int r = 0;
int a1,a2;
if (s.matches("[0-9]{1,}")) {
r=Integer.valueOf(s);
if (r==0) {
System.out.println("tret");
continue;
}
if (r<13) {
a1 = r%13;
System.out.println(s1[a1-1]);
}
if (12<r&&r<169) {
a2 = r/13;
a1 = r-a2*13;
if (a1==0) {
System.out.println(s2[a2-1]);
}else {
System.out.println(s2[a2-1]+" "+s1[a1-1]);
}
}
}else{
if (s.equals("tret")) {
System.out.println(0);
continue;
}
String[] split = s.split(" ");
if (split.length!=1) {
a1 = getIndex(s1, split[1]);
a2 = getIndex(s2, split[0]);
System.out.println(a2*13+a1);
}else {
a1 = getIndex(s1, split[0]);
a2 = getIndex(s2, split[0]);
if (a1!=0) {
System.out.println(a1);
}else {
System.out.println(a2*13);
}
}
}
}
}
public static int getIndex(String[] arr,String value){
for (int i = 0; i < arr.length; i++) {
if (arr[i].equals(value)) {
return i+1;
}
}
return 0;
}
}