1100 Mars Numbers (20分)
作者:CHEN, Yue
单位:浙江大学
代码长度限制:16 KB
时间限制:400 ms
内存限制:64 MB
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
题意:
将数字转成13进制的火星文,将火星文转成10进制的数字。
思路:
首先要判断要转换的是数字还是火星文,也不知道火星文的话是两位输入还是1位输入,所以需要读取整行,用第一个字符是否位数字作为判断。在火星文位数的判断上可以采用字符串长度或者找空格。
参考代码:
#include <iostream>
#include <string>using namespace std;
string digit[13] = {"tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};
string hdigit[13] = {"###", "tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};void fun1(int a) {if (a / 13)cout << hdigit[a / 13];if (a % 13 && a / 13)cout << " ";if (a % 13 || a == 0)cout << digit[a % 13];
}void fun2(string s) {int a = 0, b = 0;string s1 = s.substr(0, 3), s2;if (s.length() > 4)s2 = s.substr(4);for (int i = 0; i <= 12; i++) {if (s1 == hdigit[i] || s2 == hdigit[i])a = i;if (s1 == digit[i] || s2 == digit[i])b = i;}cout << a * 13 + b;
}int main() {int n;string str;scanf("%d", &n);getchar();for (int i = 0; i < n; i++) {getline(cin, str);if (isdigit(str[0])) {fun1(stoi(str));} else fun2(str);printf("\n");}return 0;
}
如有错误,欢迎指正