1100 Mars Numbers (20 分) java 题解

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];
			}
		}
	}
}

 PAT提交截图:

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值