题目
题目地址:PAT 乙级 1044
思路
简单的进制转化问题,根据题意进行相应的进制转化即可,因为题目已经划定了数据的求解范围,甚至连进制转化中的循环都不需要,进行简单计算就可以得出结果;
但本题还是有坑,结果就在这个坑上栽了很多次;10进制化为13进制的过程中,对于可以被13整除的数,后面的0需要省略,而不能打印出来,例如对于13、26这样的数,最终输出的结果是tam、hel,而不是tam tret、hel tret;
代码
#include <iostream>
#include <string>
#include <cmath>
using namespace std; const string gewei[] = { "tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec" };
const string shiwei[] = { "###", "tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou" }; void ten2tir(string str) {
int num = , D_val = ;
for (int i = ; i < str.size(); i++) {
num += (str[i] - ) * pow(, (str.size() - D_val));
D_val++;
}
if (num < )
cout << gewei[num] << endl;
else {
if (num % == )
cout << shiwei[num / ] << endl;
else
cout << shiwei[num / ] << ' ' << gewei[num % ] << endl;
}
} void tir2ten(string str) {
int loc_space = ;
int sum = ;
for (int i = ; i < str.size(); i++)
if (str[i] == ' ')
loc_space = i;
if (loc_space) {
string tmp;
tmp = str.substr(, loc_space);
for (int i = ; i < ; i++)
if (tmp == shiwei[i])
sum += i * ;
tmp = str.substr(loc_space + , str.size() - loc_space - );
for (int i = ; i <= ; i++)
if (tmp == gewei[i])
sum += i;
}
else {
for (int i = ; i < ; i++)
if (str == shiwei[i])
sum += i * ;
for (int i = ; i <= ; i++)
if (str == gewei[i])
sum += i;
}
cout << sum << endl;
} int main() {
int n = ;
string str;
cin >> n;
cin.ignore();
for (int i = ; i < n; i++) {
getline(cin, str);
if (isdigit(str[]))
ten2tir(str);
else
tir2ten(str);
} return ;
}