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;
}
网友评论