PAT A1100 / B1044 Números de Marte (20 puntos)

Enlace de tema
Nota:
1. Después de ingresar el número de conversiones n, use getchar () para absorber el carácter de nueva línea; de lo contrario, la línea se tratará como una cadena.
2. Si el número es un múltiplo entero de 13, no necesita emitir "tret". Por ejemplo, para 13, solo necesita emitir "tam", y es incorrecto emitir "tam tret".
Código AC:

#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 artículos originales publicados · Me gusta0 · Visitas 646

Supongo que te gusta

Origin blog.csdn.net/weixin_44546393/article/details/105600704
Recomendado
Clasificación