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 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 (<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:

要点:进制转换

易错点:转换成Mars Numbers时  个位的tret省略

示例:

#include

#include

#include

using namespace std;

string base1[13] = {"tret","jan","feb","mar","apr","may","jun","jly","aug","sep","oct","nov","dec"};

string base2[12] = { "tam","hel","maa","huh","tou","kes","hei","elo","syy","lok","mer","jou" };

int getIndex1(string str)

{

    for (int i = 0; i < 13; ++i)

        if (str == base1[i])

            return i;

    return -1;

}

int getIndex2(string str)

{

    for (int i = 0; i < 12; ++i)

        if (str == base2[i])

            return i + 1;

    return -1;

}

void test()

{

    int N;

    cin >> N;

    getchar();

    for (int i = 0; i < N; ++i)

    {

        string str;

        getline(cin, str);

        if (isdigit(str[0]))

        {

            int num = stoi(str);

            if (num > 12)   

            {

                  if(num%13==0)

                  cout << base2[num / 13 - 1]<< endl;

                  else

                  cout << base2[num / 13 - 1] << " " << base1[num % 13] << endl;

            }

            else

                cout << base1[num] << endl;

        }

        else

        {

            int num = 0;

            int result = 0;

            istringstream iss(str);

            while (iss >> str)

            {

                num=(getIndex1(str) > getIndex2(str) ? getIndex1(str) : getIndex2(str));

                if (getIndex2(str) > 0)

                    result += (13 * num);

                else

                    result += num;

            }

            cout << result << endl;

        }

    }

}

int main()

{

    test();

    system("pause");

    return 0;

}

你可能感兴趣的:(PAT 甲级 1100 Mars Numbers (20分))