题目:1044 火星数字 - PAT (Basic Level) Practice (中文) (pintia.cn)
解析: 1044. 火星数字(20)-PAT乙级真题 – 柳婼 の blog (liuchuo.net)
#include<iostream>
#include<string>
using namespace std;
string a[13] = {"tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};
string b[13] = {"####", "tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};
//地球文转火星文
void fun1(int x)
{
//如果是数字,就映射
//个位数应映射a,十位数映射b
if(x/13!=0)//说明是2位数
cout<<b[x/13];
//如果有余数,那就空一个格子,再输出另一个火星文
if((x/13)&&(x%13))cout<<" ";
if(x%13 || x==0)cout<<a[x%13];
}
//火星文转地球文
void fun2(string x,int len)
{
int t1=-0,t2=0;
string s1=x.substr(0,3),s2; //第一个火星文
if(len>4)s2=x.substr(4,3); //第二个火星文
for(int i=1;i<=12;i++)
{
if(s1==a[i] || s2==a[i])t2=i; //赋值个位
if (s1 == b[i]) t1 = i;
}
cout << t1 * 13 + t2;
}
int main()
{
int n=0;cin>>n;
getchar();
string s;
for(int i=0;i<n;i++) //多轮
{
getline(cin,s);
int len = s.length();
if (s[0] >= '0' && s[0] <= '9')
fun1(stoi(s));
else
fun2(s,len);
cout << endl; //每一轮都换一行
}
return 0;
}
二刷
#include<bits/stdc++.h>
using namespace std;
//火星文的1~12
string a[13]={"tret","jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};
//下标从1开始,便于处理 火星文的进位高位: 13 26 39 52 65 78 91 104 117 130
string b[20]={"","tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo","syy", "lok", "mer", "jou"};
int main()
{
int n=0;cin>>n;
getchar();
string s;
for(int i=0;i<n;i++)
{
getline(cin,s);
//首先输入的数字,要求我们转火星文
if(s[0]>='0'&&s[0]<='9')
{
int temp=stoi(s);
//没有产生进位,映射a表
if(temp<=12)cout<<a[temp]<<endl;
//产生进位 并且 是13的倍数,比如13 26,直接映射b表
else if(temp&&temp%13==0)cout<<b[temp/13]<<endl;
//产生进位 输出高位+低位
else cout<<b[temp/13]<<" "<<a[temp%13]<<endl;
}
else //输入的火星文,要求转数字
{
vector<string>v;
stringstream l(s);
string x;
while(l>>x)v.push_back(x);
int ans=0;
if(v.size()==1) //如果输入的是1位数
{
for(int i=0;i<=12;i++) if(v[0]==a[i])ans=i;
for(int i=0;i<=12;i++) if(v[0]==b[i])ans=i*13;
}
else //说明是两位数
{ //高位是进位
for(int i=1;i<=12;i++) if(v[0]==b[i])ans=i*13;
for(int i=0;i<=12;i++) if(v[1]==a[i])ans+=i;
}
cout<<ans<<endl;
}
}
return 0;
}