Mars Numbers
People on Mars count their numbers with base 13:
- Zero on Earth is called “tret” on Mars.
- The numbers 1 to 12 on Earch 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
题意
地球数字和火星数字的转换。火星数字基于13进制,用一个长度3的字符串表示一个地球数字,火星数字最多只有两位,且高位和低位相同数字的表示方式不同,要求将给定的地球(火星)数字转换成对应的火星(地球)数字并输出。
思路
整体思路上就是13进制转换和数字表示方式转换,没有什么问题。关键在细节处理上有很多坑,主要有以下几点:
- 输入的火星数字只有一位时,要判断是高位数字还是低位数字,低位直接转换输出,高位则要乘以13再输出;
- 地球数字转换成火星数字时,如果存在高位且低位为0,则不输出低位,只输出高位。
代码实现
#include <iostream>
#include <string>
#include <map>
using namespace std;
string to_M_Lowwer[13] = {"tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};
string to_M_Higher[13] = {"tret", "tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};
map<string, int> to_E_Higher, to_E_Lowwer;
void createMap() // 映射创建
{
for (int i = 0; i < 13; i++)
{
to_E_Higher[to_M_Higher[i]] = i;
to_E_Lowwer[to_M_Lowwer[i]] = i;
}
}
void solve(string s)
{
if (s[0] >= '0' && s[0] <= '9') // 输入为地球数字
{
int num = 0;
for (int i = 0; i < s.length(); i++)
num = num * 10 + s[i] - '0'; // 求具体数字
int b = num % 13; // 低位
int a = num / 13; // 高位
if (a != 0) // 存在高位
if (b != 0) // 低位不为0,高低位都要输出
cout << to_M_Higher[a] << " " << to_M_Lowwer[b] << endl;
else // 低位为0,只输出高位
cout << to_M_Higher[a] << endl;
else // 只存在低位
cout << to_M_Lowwer[b] << endl;
}
else if (s.length() == 7) // 输入为火星数字,且高低位都存在
{
string x = s.substr(0, 3); // 字符串拆分
string y = s.substr(4, 3);
int a = to_E_Higher[x];
int b = to_E_Lowwer[y];
cout << a * 13 + b << endl;
}
else if (to_E_Higher.find(s) != to_E_Higher.end()) // 只输入高位火星数字
cout << to_E_Higher[s] * 13 << endl;
else // 输入低位火星数字
cout << to_E_Lowwer[s] << endl;
}
int main()
{
int n;
string s;
createMap();
cin >> n;
getchar();
for (int i = 0; i < n; i++)
{
getline(cin, s);
solve(s);
}
return 0;
}