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 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
1 #include <iostream> 2 #include <algorithm> 3 #include <cstdio> 4 #include <cstring> 5 #include <string> 6 #include <map> 7 #include <stack> 8 #include <vector> 9 #include <queue> 10 #include <set> 11 #define LL long long 12 #define INF 0x3f3f3f3f 13 using namespace std; 14 const int MAX = 30; 15 16 int n; 17 string s; 18 char s1[MAX][6] = {"tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"}; 19 char s2[MAX][6] = {" ", "tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"}; 20 21 int my_pow(int x, int n) 22 { 23 int ans = 1; 24 while (n) 25 { 26 if (n & 1) ans *= x; 27 x *= x; 28 n >>= 1; 29 } 30 return ans; 31 } 32 33 int main() 34 { 35 // freopen("Date1.txt", "r", stdin); 36 scanf("%d", &n); 37 getchar(); 38 for (int i = 1; i <= n; ++ i) 39 { 40 getline(cin, s); 41 if (isalpha(s[0])) 42 { 43 int len = s.size(); 44 if (len >= 6) 45 { 46 char str1[6], str2[6]; 47 int a = 0, b = 0, ans = 0; 48 for (a, b; a < len; ++ a, ++ b) 49 { 50 if (s[a] == ' ') break; 51 str1[b] = s[a]; 52 } 53 str1[b] = '\0'; 54 ++ a, b = 0; 55 for (a, b; a < len; ++ a, ++ b) 56 str2[b] = s[a]; 57 str2[b] = '\0'; 58 for (int i = 0; i < 13; ++ i) 59 { 60 if (strcmp(s1[i], str2) == 0) 61 { 62 ans += i; 63 break; 64 } 65 } 66 for (int i = 1; i <= 13; ++ i) 67 { 68 if (strcmp(s2[i], str1) == 0) 69 { 70 ans += i * 13; 71 break; 72 } 73 } 74 printf("%d\n", ans); 75 } 76 else 77 { 78 char str1[10]; 79 for (int i = 0; i < len; ++ i) 80 str1[i] = s[i]; 81 str1[len] = '\0'; 82 bool flag = false; 83 for (int i = 0; i < 13; ++ i) 84 { 85 if (strcmp(str1, s1[i]) == 0) 86 { 87 flag = true; 88 printf("%d\n", i); 89 break; 90 } 91 } 92 if (flag) continue; 93 for (int i = 1; i < 13; ++ i) 94 { 95 if (strcmp(str1, s2[i]) == 0) 96 { 97 printf("%d\n", i * 13); 98 break; 99 } 100 } 101 } 102 } 103 else 104 { 105 int ans = 0, len = s.size(), t1, t2; 106 for (int i = 0, j = len - 1; i < len; ++ i, -- j) 107 ans += (s[i] - '0') * my_pow(10, j); 108 t1 = ans / 13, t2 = ans % 13; 109 if (ans <= 12) 110 printf("%s\n", s1[ans]); 111 else if (t2 == 0) 112 printf("%s\n", s2[t1]); 113 else 114 printf("%s %s\n", s2[t1], s1[t2]); 115 } 116 } 117 return 0; 118 }