PAT1100:Mars Numbers

1100. Mars Numbers (20)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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.分为地球数字转火星文、火星文转地球数字两种情况。
2.火星文转地球数字分为三种情况:1)单独一个火星数字 2)两个火星数字(第二个火星数字不是tret) 3)两个火星数字(第二个火星数字一定是tret(零))。

代码
#include<iostream>
#include<vector>
#include<string>
using namespace std;
vector<string> num = {"tret","jan","feb","mar","apr","may","jun","jly","aug","sep","oct","nov","dec"};
vector<string> highernum = {"sb","tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok","mer", "jou"};


int search(string s)
{
    for(int i = 0;i < num.size();i++)
    {
        if(s == num[i])
            return i;
    }

    for(int i = 0;i < highernum.size();i++)
    {
        if(s == highernum[i])
            return -i;
    }
    return 0;
}

int main()
{
    int N;
    scanf("%d",&N);
    getchar();
       while(N--)
       {
           string number;
           getline(cin,number);
           if(number[0] <= '9')
           {
              int n = stoi(number);
              int first = n/13,second = n % 13;
              if(n == 0)
                cout << num[n] <<endl;
              else
              {
                if(first !=0 && second != 0)
                    cout << highernum[first]<< " " <<num[second] << endl;
                else if(first != 0)
                  cout << highernum[first] << endl;
                else if(second != 0)
                  cout << num[second] << endl;
              }


           }
           else if(number.size() == 3)
           {
                int res = search(number);
                if(res >= 0)
                  cout << res <<endl;
                else
                  cout << -res * 13 << endl;
           }
           else if(number.size() == 7)
           {
               string fst = number.substr(0,3);
               string sec = number.substr(4,3);
               cout << -search(fst) * 13 + search(sec) << endl;
           }
           else
           {
               string fst = number.substr(0,3);
                cout << -search(fst) * 13 << endl;
           }
       }
}

 

posted @ 2017-10-10 17:22  0kk470  阅读(230)  评论(0编辑  收藏  举报