题目链接
思路
1、判断输入是数字还是字符
2、如果是数字,先转为13进制,由于这里规定了数字不会超过168,那么这里可以直接通过两次
取余,然后去分别打印高位和低位对应的火星文。
3、是火星文的话就去相应的数组里找下标,这里将high这个数组补成了13个元素,也可以不补(在求出相应的下标后,先i-1,然后*13)
*注意的地方:转换为13进制后,诸如10,20,30... 这样数字的0不需要输出
实现
#include <iostream>
#include <cstdio>
#include <string>
#include <cmath>
#include <vector>
typedef long long ll;
using namespace std;
const string high[13] = { "nan", "tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok",
"mer", "jou" };
const string low[13] = { "tret","jan", "feb", "mar", "apr", "may", "jun", "jly", "aug",
"sep", "oct", "nov", "dec" };
void NumToMars(string str)
{
int num = atoi(str.c_str());
if (num < 13)
cout << low[num] << endl;
else
{
cout << high[num / 13];
if (num % 13)
{
cout << " " << low[num % 13] << endl;
}
else
cout << endl;
}
}
void MarsToNum(string str)
{
int result = 0,j,i;
string strH="", strL="";
if (str.size() > 3)
{
int pos = str.find_first_of(' ');
strH = str.substr(0, pos);
strL = str.substr(pos + 1, str.size() - pos-1);
for (j = 0; j < 13; j++)
{
if (high[j] == strH)
result += j * 13;
if (low[j] == strL)
result += j;
}
}
else
{
strH = str;
for (int i = 0; i < 13; i++)
{
if (strH == high[i])
{
result += i * 13;
break;
}
if (strH == low[i])
{
result += i;
break;
}
}
}
cout << result << endl;
}
int main()
{
int N,i;
string strs[100];
scanf("%d",&N);
getchar();
for (i = 0; i < N; i++)
{
getline(cin, strs[i]);
}
for (i = 0; i < N; i++)
{
int j;
if (isdigit(strs[i][0]))
{
NumToMars(strs[i]);
}
else
{
MarsToNum(strs[i]);
}
}
return 0;
}