PAT乙级-1044 火星数字 (20分)

点击链接PAT乙级-AC全解汇总

题目:
火星人是以 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<vector>
#include<string>
#include<set>
#include<map>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<cstring>
#include<sstream>
using namespace std;
//有的时候题目是一起做的,所以会有不需要的头文件

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

void my1_to_a(string str)
{
    int num=0;
    for(int i=0;i<str.length();i++)
    {
        num=num*10+str[i]-'0';
    }
    int left=num/13;
    int right=num%13;
    //考虑只有高位没有低位的情况,所以拆开输出
    if(left>0)cout<<high[left];
    if(left>0&&right>0)cout<<" ";
    if(right>0||num==0)cout<<low[right];
    cout<<endl;
}


void mya_to_1(string str)
{
    int shi=0,ge=0;
    string left="0",right="0";
    if(str.length()>3)
    {
        right=str.substr(4,3);//low
    }
        left=str.substr(0,3);//high和low都有可能
    for(int i=0;i<13;i++)
    {
        if(left==low[i])ge=i;
        if(right==low[i])ge=i;
        if(left==high[i])shi=i;//不加的话无法识别火星文中13的倍数
    }
    printf("%d\n",shi*13+ge);

}

int main()
{
    int N;
    scanf("%d\n",&N);//记住要有换行符
    for(int i=0;i<N;i++)
    {
        string str;
        getline(cin,str);
        if(str[0]>='0'&&str[0]<='9') my1_to_a(str);
        else mya_to_1(str);
    }
    return 0;
}

注意考虑13倍数的情况,只有高位没有低位

注意考虑数字0的情况

发布了82 篇原创文章 · 获赞 1 · 访问量 1692

猜你喜欢

转载自blog.csdn.net/qq_34451909/article/details/104798826