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(<),随后 N 行,每行给出一个 [0, 169) 区间内的数字 —— 或者是地球文,或者是火星文。

输出格式:

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

输入样例:

4
29
5
elo nov
tam

输出样例:

hel mar
may
115
13

这题瞎写完写注释的时候发现自己想错了,居然歪打正着,问题不大

#include<iostream>
#include <cmath>
#include <string>
#include <bits/stdc++.h>

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

int tonum(string str){
    int num=0;
    int n=str.length();
    for(int i=0;i<n;i++){
        num=num*10+str[i]-'0';
    }
    return num;
}
void to13(string str){//转火星文 
    int x=tonum(str);//string转int 
    int m=x;
    stack<char> s;//10进制转13进制输出为string类型到str1 
    while(x){
        s.push('0'+x%13);
        x=x/13;
    }
    string str1;
    if(m==0){//排除输入0的异常情况 
        str1=str1+'0';
    }
    while(!s.empty()){
        str1=str1+s.top();
        s.pop();
    }
    if(str1.length()<2){//若是一位数 
        cout<<s1[str1[0]-'0']; 
    }else{
        if(str1[1]=='0'){
            cout<<s2[str1[0]-'0'];
        }else{
            cout<<s2[str1[0]-'0']<<" "; 
            cout<<s1[str1[1]-'0']; 
        }
        
    }
}
void to10(string str){
    if(str.length()<5){
        int i;
        for(i=0;i<55;i++){
            if(str==s1[i]){
                cout<<i;
                break;
            }else if(str==s2[i]){
                cout<<i*13;
                break;
            }
        }
    }else{
        string ss(str,0,3);
        //cout<<ss;
        int num=0;
        int i;
        for(i=0;i<55;i++){
            //cout<<i;
            if(ss==s2[i]){
                num=i*13;
                break;
            }
        }
        string sss(str,4,6);
    //    cout<<sss;
        for(i=0;i<55;i++){
            if(sss==s1[i]){
                num=num+i;
                break;
            }
        }
        cout<<num;
    }
}
int main()
{
    int N,i,j;
    cin>>N;
    getchar();  
    for(i=0;i<N;i++){
        string str;
        int sum=0;
        getline(cin,str);
        if(str[0]>='0'&&str[0]<='9'){
            to13(str);
            cout<<endl;
        }else{
            to10(str);
            cout<<endl;
        }
    } 
    return 0;
}
原文地址:https://www.cnblogs.com/siro/p/11206457.html