PAT_A1100#Mars Numbers

Source:

PAT A1100 Mars Numbers (20 分)

Description:

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 (<). 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

Keys:

  • 哈希映射

Attention:

  • getline()会吸收换行符

Code:

 1 /*
 2 题目大意:
 3 
 4 */
 5 #include<cstdio>
 6 #include<string>
 7 #include<map>
 8 #include<iostream>
 9 using namespace std;
10 const int M=13;
11 map<string,int> earth;
12 string mars0[M]={"tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};
13 string mars1[M]={"","tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};
14 
15 int main()
16 {
17 #ifdef ONLINE_JUDGE
18 #else
19     freopen("Test.txt", "r", stdin);
20 #endif // ONLINE_JUDGE
21 
22     for(int i=0; i<M; i++)
23         earth[mars0[i]]=i;
24     for(int i=1; i<M; i++)
25         earth[mars1[i]]=i*M;
26 
27     int n;
28     scanf("%d\n", &n);
29     while(n--)
30     {
31         string s;
32         getline(cin,s);
33         if(s[0]<'0' || s[0]>'9')
34         {
35             if(s.size()==3)
36                 printf("%d\n", earth[s]);
37             else
38                 printf("%d\n", earth[s.substr(0,3)]+earth[s.substr(4,3)]);
39         }
40         else
41         {
42             int num = atoi(s.c_str());
43             if(num%M!=0 && num/M!=0)
44                 printf("%s %s\n", mars1[num/M].c_str(),mars0[num%M].c_str());
45             else if(num/M!=0)
46                 printf("%s\n", mars1[num/M].c_str());
47             else
48                 printf("%s\n", mars0[num%M].c_str());
49         }
50     }
51 
52     return 0;
53 }

猜你喜欢

转载自www.cnblogs.com/blue-lin/p/11396376.html