People on Mars count their numbers with base 13:
- Zero on Earth is called "tret" on Mars.
- The numbers 1 to 12 on Earth is called "jan, feb, mar, apr, may, jun, jly, aug, sep, oct, nov, dec" on Mars, respectively.
- For the next higher digit, Mars people name the 12 numbers as "tam, hel, maa, huh, tou, kes, hei, elo, syy, lok, mer, jou", respectively.
For examples, the number 29 on Earth is called "hel mar" on Mars; and "elo nov" on Mars corresponds to 115 on Earth. In order to help communication between people from these two planets, you are supposed to write a program for mutual translation between Earth and Mars number systems.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (<100). Then N lines follow, each contains a number in [0, 169), given either in the form of an Earth number, or that of Mars.
Output Specification:
For each number, print in a line the corresponding number in the other language.
Sample Input:
4
29
5
elo nov
tam
Sample Output:
hel mar
may
115
13
解题思路:
题目大意:十进制和十三进制在【0,169】范围内的相互转化。
10->13:将10进制分别对13求商和余数,对商和除数能否为0四种情况分别讨论。
13->10:将十三进制的“个位”“十位”分别对应的数字装入map中,直接提取即可。
java代码:
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
for(int i = 0; i < n;i++) {
String temp = br.readLine();
if(Character.isDigit(temp.charAt(0))) {
System.out.println(convertToString(temp));
}else {
System.out.println(convertToInt(temp));
}
}
}
public static int convertToInt(String temp) {
String[] split = temp.split(" ");
Map<String,Integer> map = new HashMap<>();
map.put("tret", 0);
map.put("jan", 1);
map.put("feb", 2);
map.put("mar", 3);
map.put("apr", 4);
map.put("may", 5);
map.put("jun", 6);
map.put("jly", 7);
map.put("aug", 8);
map.put("sep", 9);
map.put("oct", 10);
map.put("nov", 11);
map.put("dec", 12);
map.put("tam", 1 * 13);
map.put("hel", 2 * 13);
map.put("maa", 3 * 13);
map.put("huh", 4 * 13);
map.put("tou", 5 * 13);
map.put("kes", 6 * 13);
map.put("hei", 7 * 13);
map.put("elo", 8 * 13);
map.put("syy", 9 * 13);
map.put("lok", 10 * 13);
map.put("mer", 11 * 13);
map.put("jou", 12 * 13);
if(split.length == 1) {
return map.get(split[0]);
}else {
return (map.get(split[0]) + map.get(split[1]));
}
}
public static String convertToString(String temp) {
String flagDiv[] = {"","tam","hel","maa","huh","tou","kes","hei","elo","syy","lok","mer","jou"};
String flagMod[] = {"","jan","feb","mar","apr","may","jun","jly","aug","sep","oct","nov","dec"};
int num = Integer.parseInt(temp);
int div = num / 13;
int mod = num % 13;
if(div == 0) {
if(mod == 0) {
return "tret";
}else {
return flagMod[mod];
}
}else {
if(mod == 0) {
return flagDiv[div];
}else {
return flagDiv[div] + " " + flagMod[mod];
}
}
}
}