美文网首页
PAT甲级A1100---map的常见用法

PAT甲级A1100---map的常见用法

作者: 1nvad3r | 来源:发表于2020-07-23 09:21 被阅读0次

1100 Mars Numbers (20分)

1100
分析:

先将所有映射都预处理出来,然后直接查询。

C++:
#include <iostream>
#include <iostream>
#include <map>
#include <string>

using namespace std;

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

string numTostr[170];
map<string, int> strToNum;

void init() {
    for (int i = 0; i < 13; i++) {
        numTostr[i] = unitDigit[i];//十位为0
        strToNum[unitDigit[i]] = i;
        numTostr[i * 13] = tenDigit[i];//个位为0
        strToNum[tenDigit[i]] = i * 13;
    }
    for (int i = 1; i < 13; i++) {//十位
        for (int j = 1; j < 13; j++) {//个位
            string str = tenDigit[i] + " " + unitDigit[j];
            numTostr[i * 13 + j] = str;
            strToNum[str] = i * 13 + j;
        }
    }
}

int main() {
    init();
    int n;
    scanf("%d", &n);
    getchar();//前有scanf后有getline,需要用getchar()接收换行符
    string str;
    for (int i = 0; i < n; i++) {
        getline(cin, str);
        if (str[0] >= '0' && str[0] <= '9') {
            int num = 0, product = 1;//字符串转数字
            for (int i = str.length() - 1; i >= 0; i--) {
                num += (str[i] - '0') * product;
                product *= 10;
            }
            cout << numTostr[num] << endl;
        } else {
            cout << strToNum[str] << endl;
        }
    }
    return 0;
}

相关文章

网友评论

      本文标题:PAT甲级A1100---map的常见用法

      本文链接:https://www.haomeiwen.com/subject/emhclktx.html