PAT-B 1044. 火星数字(20)

题目连接在此。

本题只需根据题意模拟即可:
打好两张表,分别是整数映射火星文、火星文映射整数,之后对输入进行处理,直接查表输出即可。

需要注意的一点是:火星文中,十位不为0,个位为0时,只输出十位。

#include
#include
#include
#include
#include

using namespace std;

char low1[13][5] = {"tret","jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};
char high1[13][5] = {"tret","tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};

map<string,int> low2;
map<string,int> high2;

int stringToInt(string str){
    int res = 0;
    int len = 1;
    while(str.length() != 0){
        res = res + (str[str.length()-1]-'0')*len;
        len *= 10;
        str.erase(str.length()-1); 
    }

    return res;
}

void dealMars(string marsNum){
    int pos = marsNum.find(' ');
    int res = 0; 
    if(pos == string::npos){  //没有空格,即仅有一位火星字 
        if(low2.find(marsNum) != low2.end()){  //如果在低位中找到 
            res += low2[marsNum];
            cout << res << endl; 
            return;
        }
        if(high2.find(marsNum) != high2.end()){  //如果在高位中找到 
            res += (high2[marsNum]*13);
            cout << res << endl;
            return;
        }
    } else{  //如果有空格,说明有两位火星数字
        string first = marsNum.substr(0, pos);
        string second = marsNum.substr(pos+1, marsNum.length());

        res += (high2[first]*13);
        res += (low2[second]);
    }
    cout << res << endl;
}

int main(){

    //对map进行初始化
    for(int i = 0; i < 13; i++){
        low2[low1[i]] = i;
        high2[high1[i]] = i;
    }

    int n;
    cin >> n;
    getchar();

    string query;

    for(int i = 0; i < n; i++){

        getline(cin, query);

        if(query[0] >= '0' && query[0] <= '9'){   //如果query的第一位是数字,说明输入的是地球文 
            int number = stringToInt(query);  //将string型输入转换成int型十进制整数 
            if(number < 13){
                cout << low1[number%13] << endl;
            }else if(number % 13 == 0){
                cout << high1[number/13] << endl; 
            }else{
                cout << high1[number/13] << " " << low1[number%13] << endl;
            } 

        } else{  //如果query的第一位不是数字,说明数的是火星文 
            dealMars(query);
        } 
    } 
    return 0;
} 

你可能感兴趣的:(PAT,(Basic,Level),Practice)