Author: CHEN, Yue
Organization: 浙江大学
Time Limit: 400 ms
Memory Limit: 64 MB
Code Size Limit: 16 KB
B1044/A1100. 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 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
Code
#pragma warning(disable:4996)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <map>
using namespace std;
string spl[13] = { "tret","jan","feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec" }
, sph[13] = { "tret", "tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou" };
char acc[10];
map<string, int> mpl, mph;
void init(){
for (int i = 0; i < 13; i++)
mpl[spl[i]] = mph[sph[i]] = i;
}
void etom(){
string a = acc, b;
int num = 0, flag = 1;
for (int i = 0; i < a.length(); i++){
if (a[i] == ' '){
b = a.substr(0, i);
a.erase(a.begin(), a.begin() + i + 1);
break;
}
}
if (b.length()){
for (int i = 0; i <13; i++){
if (b == sph[i]){
num += mph[b] * 13;
break;
}
}
}
for (int i = 0; i < 13; i++){
if (a == spl[i]){
num += mpl[a];
flag = 0;
break;
}
}
if (flag){
for (int i = 0; i < 13; i++){
if (a == sph[i]){
num += mph[a] * 13;
break;
}
}
}
printf("%d\n", num);
}
void mtoe(){
int num = 0, result[2] = { 0 }, cnt = 0;
for (int i = 0; i < strlen(acc); i++)
num = num * 10 + acc[i] - '0';
while (num){
result[cnt++] = num % 13;
num /= 13;
}
if (result[1] && result[0]) printf("%s %s\n", sph[result[1]].c_str(), spl[result[0]].c_str());
else if (result[1]) printf("%s\n", sph[result[1]].c_str());
else printf("%s\n", spl[result[0]].c_str());
}
int main(){
init();
int n;
scanf("%d", &n);
getchar();
for (int i = 0; i < n; i++){
fgets(acc, 10, stdin);
if (acc[strlen(acc) - 1] == '\n')
acc[strlen(acc) - 1] = '\0';
if ((acc[0] - '0')>10) etom();
else mtoe();
}
system("pause");
return 0;
}
Analysis
-“tam”的输出应为13,13的输出应为“tam”。