PAT 甲級(jí) 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:

要點(diǎn):進(jìn)制轉(zhuǎn)換

易錯(cuò)點(diǎn):轉(zhuǎn)換成Mars Numbers時(shí)? 個(gè)位的tret省略

示例:

#include <iostream>

#include <string>

#include <sstream>

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;

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容