1100 Mars Numbers (20分)
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进制进行数值转换。对输入的值,若小于“999”,则是数字->火星文。否则火星文->数字。
输入值用的是string,所以要有转成10进制数值的功能函数。cstring头文件有stoi(string)。但是自己也实现了一下。
这里得定义一个,Mars数据结构:高位,低位。这样子便于计算。
在编写一个Mars->10进制数的功能函数。
最后计算之后,输出结果。
#include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<list>
#include<math.h>
using namespace std;
//不能存在空格!!!
string lowrelation[13] = {"tret","jan", "feb", "mar", "apr", "may", "jun", "jly","aug", "sep", "oct", "nov", "dec"};//13
string highrelation[13] = {"","tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};//13
struct Mars {
string high,low;
Mars():high(""),low("") { };
};
int transform(Mars m) {
int sum = 0;
for(int i = 0; i < 13; i++) {
if(m.high == highrelation[i]) {
sum += i*13;
break;
}
}
for(int i = 0; i < 13; i++) {
if(m.low == lowrelation[i]) {
sum += i;
break;
}
}
return sum;
}
Mars transform(int n) {
Mars m;
if(n % 13 != 0 || n == 0)
m.low = lowrelation[n % 13];
n /= 13;
m.high = highrelation[n];
return m;
}
void print(int m) {
cout<<m<<endl;
}
void print(Mars m) {
if(m.high != "" && m.low != "") {
cout<<m.high<<" "<<m.low<<endl;
} else if(m.high == "") {
cout<<m.low<<endl;
} else {
cout<<m.high<<endl;
}
}
int stringToInt(string s) {
int sum = 0;
for(int i = 0 ; i < s.size(); i++) {
sum += (s[i] - '0') * pow(10,s.size() - 1 - i);
}
return sum;
}
int main() {
string temp;
int N;
cin>>N;
getchar();
while(N--) {
getline(cin,temp);
if(temp < "999") {
print(transform(stringToInt(temp)));
} else {
Mars m;
int pos = temp.find(" ");
if(pos < 0) {
m.low = temp;
for(int i = 0; i < 13; i++) {
if(m.low == highrelation[i]) {
m.high = m.low;
m.low = "";
}
}
} else {
m.high = temp.substr(0,pos);
m.low = temp.substr(pos+1,temp.size() - 1 - pos);
}
print(transform(m));
}
}
return 0;
}