PAT菜鸡进化史_乙级_1044

PAT菜鸡进化史_乙级_1044

火星人是以 13 进制计数的:

地球人的 0 被火星人称为 tret。
地球人数字 112 的火星文分别为:jan, feb, mar, apr, may, jun, jly, aug, sep, oct, nov, dec。
火星人将进位以后的 12 个高位数字分别称为:tam, hel, maa, huh, tou, kes, hei, elo, syy, lok, mer, jou。

例如地球人的数字 29 翻译成火星文就是 hel mar;而火星文 elo nov 对应地球数字 115。为了方便交流,请你编写程序实现地球和火星数字之间的互译。

输入格式:

输入第一行给出一个正整数 N(<100),随后 N 行,每行给出一个 [0, 169) 区间内的数字 —— 或者是地球文,或者是火星文。

输出格式:

对应输入的每一行,在一行中输出翻译后的另一种语言的数字。

输入样例:

4
29
5
elo nov
tam

输出样例:

hel mar
may
115
13

思路:

这题主要的麻烦在火星文转中文身上。。
中文转火星文中要注意的一点是tret的0仅限于数字0,像20 30 之类个位数是0的不算!!
于是乎这个0就造成了火星文只出现三个字母的时候不确定到底是个位数还是整十的数,需要都判断一下的

Code:

#include <iostream>
#include <string>
#include <cctype>
#include <vector>
#include <algorithm>
std::string E2M(std::string str);
int M2E(std::string str);
using namespace std;

int main(){
// input the number
    int n;
    cin >> n;
    cin.get();
    vector<string> ori_num(n);
    for (int i = 0; i < n; i++){
        getline(cin, ori_num[i]);
    }
// translate the number
    for (int i = 0; i < n; i++){
        if (isdigit(ori_num[i][0]))
            cout << E2M(ori_num[i]) << endl;
        else
            cout << M2E(ori_num[i]) << endl;
    }

    return 0;
}

string E2M(string str){ // Earth to Mars
    int num = stoi(str);
    int tens_digit = num / 13;
    int ones_digit = num % 13;
    string tens[12] = {"tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};
    string ones[13] = {"tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};

    if (tens_digit != 0 && ones_digit != 0)
        return tens[tens_digit - 1] + " " + ones[ones_digit];
    else if (tens_digit != 0)
        return tens[tens_digit - 1];
    else
        return ones[ones_digit];
}
int M2E(string str){ // Mars to Earth
    string tens[13] = {"****", "tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};
    string ones[13] = {"tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};
    string str1, str2;
    if (str.size() == 4)
        return 0;
    str1 = str.substr(0, 3);
    if (str.size() > 4)
        str2 = str.substr(4, 3);
    int tens_digit = 0, ones_digit = 0;
    for (int i = 0; i < 13; i++){
        if (str1 == tens[i])
            tens_digit = i;
        else if (str1 == ones[i])
            ones_digit = i;
        if (str2 == ones[i])
            ones_digit = i;
    }
    return tens_digit * 13 + ones_digit;
}

猜你喜欢

转载自blog.csdn.net/Raccoonnn/article/details/88171769