题目:
https://pintia.cn/problem-sets/994805260223102976/problems/994805279328157696
直接贴代码:
#include <iostream>
#include <string>
using namespace std;
void fun1(string t);
void fun2(string t);
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"};
int tt,temp,temp1,ans = 0;
int main(){
string t;
int n;
cin>>n;
getchar();
for(int i = 0;i<n;i++){
getline(cin,t);
if(t[0]>='0'&&t[0]<='9')fun1(t);//数字转火星文
else fun2(t);//火星文转数字
cout<<endl;
}
return 0;
}
void fun1(string t){
int len = t.length();
int num = 0;
for(int i = 0;i<len;i++)//将字符串变成数字
{
num = num*10+(t[i]-'0');
}
cout<<b[num/13];
if((num%13)&&(num/13))cout<<" "<<a[num%13];//如果有进位和低位
else if(num%13)cout<<a[num%13];//没有进位
else if(num==0)cout<<"tret";//为零的情况
}
void fun2(string t){
if(t.size()==3)//只有一个火星文
{
for(int i = 0;i<13;i++){//遍历高位
if(t==b[i]){
cout<<i*13;
break;
}
}
for(int i = 0;i<13;i++){//遍历低位
if(t==a[i]){
cout<<i;
}
}
}
else if(t.size()==4)cout<<"0";//有四位肯定是零
else //有两个火星文
{
string aa = t.substr(0,3);//高位火星文
string bb = t.substr(4,3);//低位火星文
for(int i = 0;i<13;i++){
if(aa==b[i])temp = i*13;
if(bb==a[i])temp1 = i;
}
cout<<temp+temp1;
}
}
PAT乙级1044火星数字—字符串的数据处理(不会写)
最新推荐文章于 2021-02-26 14:41:07 发布