PAT甲级刷题记录——1100 Mars Numbers (20分)

People on Mars count their numbers with base 13:

  • Zero on Earth is called “tret” on Mars.
  • The numbers 1 to 12 on Earth 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.

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.

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

思路

这题看上去挺绕的,但其实就是一个进制转换的问题,这里所谓的“火星”上的进制应该是13进制,但是题目给的要求又和平时的进制转换不太一样,当是13的倍数的时候,要求是只有一个数(比如,13是tam,按理来说13应该是tam tret,然后14是tam jan……以此类推),因为范围比较小,只有到168(jou dec就是168,也就是12×131+12×130,实在不好理解的可以想一下16进制的FF,两个意思是差不多的),因此直接预处理打表就好了(放在两个map里,分别是火星转地球和地球转火星),然后给一个输入,就查询一次。

另外,这里强烈建议用字符串数组,而不要使用char的二维数组(虽然char的二维数组也能表示字符串数组,但是并不支持+号操作,因为没有重载),因为字符串支持+号操作,这样的话如果碰到是两位的火星文,中间的空格就能直接加上去了(比如"jou"+" “+“dec”,得到"jou dec”,字符串的+号操作真的非常方便)。

代码

#include<cstdio>
#include<cctype>
#include<stdlib.h>
#include<algorithm>
#include<string>
#include<map>
#include<string.h>
#include<iostream>
using namespace std;
map<string, int> MToE;
map<int, string> EToM;
string gewei[13]={
"tret", "jan", "feb", "mar", "apr", "may",
"jun", "jly", "aug", "sep", "oct", "nov", "dec"
};
string shiwei[13]={
"0", "tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"
};
int main(){
    for(int i=0;i<=168;i++){
        if(i>=0&&i<=12){
            MToE[gewei[i]] = i;
            EToM[i] = gewei[i];
        }
        else{
            int yushu = i%13;
            int gaowei = i/13;
            string tmp;
            if(yushu==0) tmp = shiwei[gaowei];
            else tmp = shiwei[gaowei]+" "+gewei[yushu];
            MToE[tmp] = i;
            EToM[i] = tmp;
        }
    }
    int N;
    cin>>N;
    cin.ignore();//getline之前如果有cin,一定要ignore换行符
    for(int i=0;i<N;i++){
        string temp;
        getline(cin, temp);
        if(isalpha(temp[0])==false){//如果不是字母,也就是说是数字的话
            cout<<EToM[stoi(temp)]<<'\n';
        }
        else{
            cout<<MToE[temp]<<'\n';
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值