1100 Mars Numbers——PAT甲级真题

1100 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进制,在火星上‘0’用“tret"表示,其中底12位为"tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"。进位后的高位表示为"tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"(13 * 1 == tam, 13 * 2 == hel 一次类推)。例如:29 /13 = 2,13 * 2 == hel, 29 % 13 = 3. 3 == mar. "elo nov" = 13 * 8 + 11. 题目中给出的数字最大为169,说明13进制表示的最高位为2位。

大致思路:首先我们要判断输入的是数字还是字符。

  1. 输入的是数字:假设输入数字t,如果t / 13 > 0说明数字> 13. t / 13表示高位数字,如果t % 13 != 0 用来表示地位数字。最后注意判断t == 0的特殊情况。
  2. 输入的是字符:先判断字符长度,如果字符长度 ==3说明只有地位数字,直接在底12位查找其对应的位。如果字符长度 > 3.分别截取地位和高位字符,然后分别查找其对应的位。

代码:

#include <bits/stdc++.h>

using namespace std;

string ch1[13] = {"tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};
string ch2[13] = {"####", "tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"}; //数字转日期
void todate(string str) {
int tmp = stoi(str);
if (tmp / 13) cout << ch2[tmp / 13];
if ((tmp / 13) && (tmp % 13)) cout << " ";
if (tmp % 13 || tmp == 0) cout << ch1[tmp % 13];
} //字符转数字
void tonum(string str) {
int ans1 = 0, ans2 = 0; //ans1记录低位数字,ans2记录高位数字
string t1, t2;
t1 = str.substr(0, 3);
if (str.size() > 4) t2 = str.substr(4, 3);
for (int i = 0; i < 13; i++) {
if (ch1[i] == t2 || ch1[i] == t1) ans1 = i;
if (ch2[i] == t1) ans2 = i;
}
// cout << ans2 << " " << ans1 << endl;
cout << ans2 * 13 + ans1;
} int main() {
int n;
cin >> n;
getchar();
while(n--) {
string num;
getline(cin, num);
if (num[0] >= '0' && num[0] <= '9') todate(num);
else tonum(num);
cout << endl;
}
return 0;
}
上一篇:hi3531spi flash启动和bootrom启动的对比


下一篇:pat1100. Mars Numbers (20)