PAT-A1100/B1044 Mars Numbers/火星数字 题目内容及题解

People on Mars count their numbers with base 13:

  • Zero on Earth is called "tret" on Mars.
  • The numbers 1 to 12 on Earch is called "jan, feb, mar, apr, may, jun, jly, aug, sep, oct, nov, dec" on Mars, respectively.
  • For the next higher digit, Mars people name the 12 numbers as "tam, hel, maa, huh, tou, kes, hei, elo, syy, lok, mer, jou", respectively.

For examples, the number 29 on Earth is called "hel mar" on Mars; and "elo nov" on Mars corresponds to 115 on Earth. In order to help communication between people from these two planets, you are supposed to write a program for mutual translation between Earth and Mars number systems.

火星人是以 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。为了方便交流,请你编写程序实现地球和火星数字之间的互译。

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (<100). Then N lines follow, each contains a number in [0, 169), given either in the form of an Earth number, or that of Mars.

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

Output Specification:

For each number, print in a line the corresponding number in the other language.

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

Sample Input:

4
29
5
elo nov
tam

Sample Output:

hel mar
may
115
13

解题思路

  1. 用map容器建立字符串到数字的对应关系,用二维数组建立数字到字符串的对应关系;
  2. 读入数据并按照规则将其进行转化并输出。
  3. 返回零值。

代码

#include<cstdio>
#include<cstring>
#include<string>
#include<map>
using namespace std;

char one[13][5]={"tret","jan","feb","mar","apr","may","jun","jly","aug","sep","oct","nov","dec"};
char dec[13][5]={"","tam","hel","maa","huh","tou","kes","hei","elo","syy","lok","mer","jou"};
map<string,int> mp;

int N;
void Init(){
    mp["tret"]=0;
    mp["jan"]=1;
    mp["feb"]=2;
    mp["mar"]=3;
    mp["apr"]=4;
    mp["may"]=5;
    mp["jun"]=6;
    mp["jly"]=7;
    mp["aug"]=8;
    mp["sep"]=9;
    mp["oct"]=10;
    mp["nov"]=11;
    mp["dec"]=12;
    mp["tam"]=13;
    mp["hel"]=26;
    mp["maa"]=39;
    mp["huh"]=52;
    mp["tou"]=65;
    mp["kes"]=78;
    mp["hei"]=91;
    mp["elo"]=104;
    mp["syy"]=117;
    mp["lok"]=130;
    mp["mer"]=143;
    mp["jou"]=156;
    scanf("%d",&N);
    getchar();
}

void numtochar(char a[]){
    int i=0,n=0;
    int len=strlen(a);
    while(i<len){
        n=n*10+a[i]-'0';
        i++;
    }
    if(n==0){
        printf("tret\n");
        return;
    }
    if(n>12){
        printf("%s",dec[n/13]);
        n%=13;
        if(n>0){
            printf(" ");
        }else{
            printf("\n");
            return;
        }
    }
    if(n>0){
        printf("%s\n",one[n]);
    }
}

void chartonum(char a[]){
    int i,j=0;
    int sum=0;
    string c;
    int len=strlen(a);
    char b[10];
    if(len>6){
        i=0;
        while(a[j]!=' '){
            b[i++]=a[j];
            j++;
        }
        b[i]=0;
        c=b;
        sum+=mp[c];
        j++;
    }
    i=0;
    while(j<len){
        b[i++]=a[j];
        j++;
    }
    b[i]=0;
    c=b;
    sum+=mp[c];
    printf("%d\n",sum);
}

int main(){
    int i,len;
    char a[20];
    Init();
    for(i=0;i<N;i++){
        fgets(a,20,stdin);
        len=strlen(a);
        a[len-1]=0;
        if(a[0]>='0'&&a[0]<='9'){
            numtochar(a);
        }else{
            chartonum(a);
        }
    }
}

运行结果

PAT-A1100/B1044 Mars Numbers/火星数字 题目内容及题解