1044. 火星数字(20)

1044. 火星数字(20)

火星人是以13进制计数的:

地球人的0被火星人称为tret。
地球人数字1到12的火星文分别为: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
#include <iostream>
#include <stack>
#include <cstdio>
#include <map>
#include <cstdlib>
#include <vector>
#include <cmath>
using namespace std;

string gewei_array[]={"tret","jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};
string gaowei_array[]={"tret","tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};

int main()
{
    map<string,int> gewei;
    map<string,int> gaowei;
    for(int i=0;i<13;i++)
    {
        gewei.insert(map<string, int> :: value_type(gewei_array[i], i));
    }
    for(int i=0;i<13;i++)
    {
        gaowei.insert(map<string,int>::value_type(gaowei_array[i],i));
    }

    int n;
    cin>>n;
    cin.clear();
    cin.ignore();
    for(int i=0;i<n;i++)
    {
        string str;
        getline(cin,str);
        //地球文
        if(str[0]>='0'&&str[0]<='9')
        {
            stack<int> ans;
            int k=atoi(&str[0]);
            if(k==0)
            {
                cout<<"tret"<<endl;
                continue;
            }
            while(k>0)
            {
                ans.push(k%13);
                k/=13;
            }
            //输出结果
            int first_flag=1;
            while(ans.size()>1)
            {
                if(first_flag==0)
                {
                    cout<<" ";
                }
                first_flag=0;
                cout<<gaowei_array[ans.top()];
                ans.pop();
            }
            //火星文最后一个0不输出
            if(ans.top()==0)
            {
                cout<<endl;
            }
            else
            {
                if(first_flag==0)
                {
                    cout<<" ";
                }
                cout<<gewei_array[ans.top()]<<endl;
            }
            
            //end...
        }
        //火星文
        else
        {
            //分割字符串
            vector<string> ans;
            str+=" ";//追加一个空格在末尾;
            size_t pos =str.find_first_of(' ',0);
            while(pos!=string::npos)
            {
                string sub=str.substr(0,pos);
                ans.push_back(sub);
                str.erase(0,pos+1);
                pos=str.find_first_of(' ',0);
            }
            int sum=0;
            int mi=0;
            map<string,int>::iterator it=gewei.find(ans.back());
            if(it!=gewei.end())
            {
                sum+=pow(13,mi)*(gewei.find(ans.back())->second);
                ans.pop_back();
            }
            
            mi=1;
            while(ans.size()>0)
            {
                sum+=pow(13.,mi)*(gaowei.find(ans.back())->second);
                ans.pop_back();
                mi++;
            }
            
            cout<<sum<<endl;
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/xiongmao-cpp/p/6377653.html