PAT 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

思路:用string数组来保存火星数, 用map保存火星数对应的阿拉伯数字;
当输入时火星数,判断是有还有第二个输入的办法:如果输入的第一个字符串是0-12对应的火星数,则肯定没有第二个字符串输入;
若不满足上以条件, 则再吸收一个字符,判断是否是'\n', 若是则表示输入结束, 反之则表示还有一个字符串
当输入的是阿拉伯数字的时候, 只需要判断第一位是否是0-9的字符就能判断, 字符串的长度,由'\0'出现的位置决定
注意点:单独处理输入数字为0的情况
 1 #include<iostream>
 2 #include<vector>
 3 #include<map>
 4 using namespace std;
 5 string ch1[]={"tret","jan","feb","mar","apr","may","jun","jly","aug","sep","oct","nov","dec"};
 6 string ch2[]={"","tam","hel","maa","huh","tou","kes","hei","elo","syy","lok","mer","jou"};
 7 map<string, int> m1, m2;
 8 
 9 int main(){
10   int n, i;
11   cin>>n;
12   for(i=0; i<13; i++) m1[ch1[i]] = i;
13   for(i=0; i<13; i++) m2[ch2[i]] = i*13;
14   for(i=0; i<n; i++){
15     string a, b;
16     cin>>a;
17     if(a[0]<='9' && a[0]>='0'){
18       int temp=0, cnt=0;
19       while(a[cnt]!='\0') temp = temp*10 + (a[cnt++]-'0');
20       if(temp/13) {
21         cout<<ch2[temp/13];
22         if(temp%13) printf(" ");
23         else cout<<endl;
24       }
25       if(temp%13) cout<<ch1[temp%13]<<endl;
26       if(temp==0) cout<<ch1[0]<<endl;
27     }else{
28       int sum=0;
29       if(m2.count(a)){
30         sum += m2[a];
31         if(cin.get()!='\n') cin>>b;
32         sum += m1[b];;
33       }else sum += m1[a];
34       cout<<sum<<endl;
35     }
36   }
37   return 0;
38 }

 

posted @ 2018-06-27 08:36  赖兴宇  阅读(118)  评论(0编辑  收藏  举报