PAT A1100/B1044 Mars Numbers(火星数字) (20point(s))

题目链接
注意点:
1.输入转换个数n之后要用getchar()吸收掉换行符,否则会将那行当做字符串处理。
2.如果数字是13的整倍数,则不用输出"tret",如对于13,只要输出"tam"就可,输出"tam tret"就错了。
AC code:

#include<cstdio>
#include<string>
#include<map>
#include<iostream>
using namespace std;
string earthTOmars_low[13]={"tret","jan","feb","mar","apr","may","jun","jly","aug","sep","oct","nov","dec"};
//低位数字到火星文的转换
string earthTOmars_high[13]={"tret","tam","hel","maa","huh","tou","kes","hei","elo","syy","lok","mer","jou"};
//高位数字到火星文的转换
map<string,int> marsTOearth;
//火星文到十进制数的映射
int main(){
for(int i=0;i<13;i++){//映射初始化
    marsTOearth[earthTOmars_low[i]]=i;
    marsTOearth[earthTOmars_high[i]]=13*i;
}
int n;
cin>>n;
getchar();//吸收换行符
for(int i=0;i<n;i++){
    string temp;
    int num=0;
    getline(cin,temp);//输入待翻译的数
    if(temp[0]>='0'&&temp[0]<='9'){//是十进制数
        for(int i=0;i<temp.length();i++){
            num=num*10+(temp[i]-'0');//转换为int型
        }
          int x=num/13;//高位
          int y=num%13;//低位
          if(x&&y)//高位低位都有,要用空格隔开输出
          cout<<earthTOmars_high[x]<<" "<<earthTOmars_low[y]<<"\n";
          else if(x)//只有高位,单独输出
          cout<<earthTOmars_high[x]<<"\n";
          else//只有低位,单独输出
          cout<<earthTOmars_low[y]<<"\n";
    }
    else{//是火星文
        if(temp.length()>=0&&temp.length()<=3){//只有一个火星文
            cout<<marsTOearth[temp]<<endl;
        }
        else{//两个火星文
            num=marsTOearth[temp.substr(0,3)]+marsTOearth[temp.substr(4,3)];
            //将低位与高位相加
            cout<<num<<endl;
        }
    }
}
return 0;
}
发布了81 篇原创文章 · 获赞 0 · 访问量 646

猜你喜欢

转载自blog.csdn.net/weixin_44546393/article/details/105600704
今日推荐