PAT (Basic Level) Practice (中文)1044 火星数字

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<cstdio>
#include<ctype.h>
using namespace std;

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

int main()
{   
    //小的两个字符串 
    string str1,str2;
    int many=0;
    cin>>many;
    string str[many];
    getchar();

    int temp[300]={0};
    int tempnum=0; 
    int index=0;
    int num=0;
    for(int i=0;i<many;i++)
    {

        getline(cin,str[i]);
    //  cin>>str[i];

    }

    //逐条处理string 
    for(int i=0;i<many;i++)
    {
        //初始化数据 
        num=0;
        index=0;
        tempnum=0;
        str1="";
        str2="";

        if(isalpha(str[i][0]))
        {
            int t1=0,t2=0;
            if(str[i].length()==3)
            {
                str1=str[i].substr(0,3);
                for(int j=1;j<=12;j++)
                {

                    if(huo1[j]==str1)
                    {
                        t2=j;
                        cout<<t2<<endl;
                    }
                    if(huo2[j]==str1)
                    {
                        t2=j;
                        cout<<t2*13<<endl;
                    }   
                }

            }
            else
            {
                str1=str[i].substr(0,3);
                str2=str[i].substr(4,3);
                for(int j=1;j<=12;j++)
                {
                    if(huo2[j]==str1)
                    {
                        t1=j;
                    }
                    if(huo1[j]==str2)
                    {
                        t2=j;
                    }

                }
                cout<<t1*13+t2<<endl;
            }

        }
        //如果是数字,代表着这一串肯定是地球文, 先提取出数字,然后再把这个数字转换为13进制 
        if(isdigit(str[i][0]))
        {
            //提取出数字 
            for(int j=0;j<str[i].size();j++)
            {
                num=num*10+str[i][j]-'0';
            } 

            if(num<13)
            {
                cout<<huo1[num]<<endl;
            }
            else if(num%13==0)
            {
                cout<<huo2[num/13]<<endl;
            }
            else
            {
                cout<<huo2[num/13]<<" "<<huo1[num%13]<<endl;
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/hhmy77/article/details/81945809