PAT乙1044未解

1044 火星数字(20)(20 分)
火星人是以13进制计数的:

地球人的0被火星人称为tret。
地球人数字112的火星文分别为:jan, feb, mar, apr, may, jun, jly, aug, sep, oct, nov, dec。
火星人将进位以后的12个高位数字分别称为:tam, hel, maa, huh, tou, kes, hei, elo, syy, lok, mer, jou。
例如地球人的数字“29”翻译成火星文就是“hel mar”;而火星文“elo nov”对应地球数字“115”。为了方便交流,请你编写程序实现地球和火星数字之间的互译。

输入格式:

输入第一行给出一个正整数N(<100),随后N行,每行给出一个[0, 169)区间内的数字 —— 或者是地球文,或者是火星文。

输出格式:

对应输入的每一行,在一行中输出翻译后的另一种语言的数字。

输入样例:

4
29
5
elo nov
tam
输出样例:

hel mar
may
115
13

gets可以输入镶嵌空格的字符串,但是这里用scanf(“%s%c”)
这里写图片描述

#include<iostream>
#include<cstdio>
#include<map>
using namespace std;
int n,r;
char f[4],c;
string e_m[2][13]={"tret","jan","feb","mar","apr","may","jun","jly","aug","sep","oct","nov","dec","x","tam","hel","maa","huh","tou","kes","hei","elo","syy","lok","mer","jou"};
map<string,int>m_e;
int main()
{
    m_e["tret"]=0;m_e["jan"]=1;m_e["feb"]=2;m_e["mar"]=3;m_e["apr"]=4;m_e["may"]=5;m_e["jun"]=6;m_e["jly"]=7;m_e["aug"]=8;m_e["sep"]=9;m_e["oct"]=10;m_e["nov"]=11;m_e["dec"]=12;
    m_e["tam"]=13;m_e["hel"]=26;m_e["maa"]=39;m_e["huh"]=52;m_e["tou"]=65;m_e["kes"]=78;m_e["hei"]=91;m_e["elo"]=104;m_e["syy"]=117;m_e["lok"]=130;m_e["mer"]=143;m_e["jou"]=156;
    scanf("%d",&n);
    while(n--)
    {
        r=0;
        while(1)
        {
            scanf("%s%c",&f,&c);
            if(c=='\n')
            {
                if(f[0]>'_')
                {
                    r+=m_e[f];
                    printf("%d\n",r);
                }
                else
                {
                    int i=-1;
                    while(f[++i]!='\0')
                    {
                        r=r*10+f[i]-'0';
                    }
                    if(r==0)
                    {
                        printf("tret\n");
                        continue;
                    }
                    int t=r/13;
                    r%=13;
                    if(t)cout<<e_m[1][t];
                    if(t&&r)cout<<' ';
                    if(r)cout<<e_m[0][r%13];
                    cout<<endl;
                }
                break;
            }
            else
            {
                r+=m_e[f];
            }
        }
    }
    return 0;
} 

猜你喜欢

转载自blog.csdn.net/weixin_40367307/article/details/81165180
今日推荐