【PAT甲级】1100 Mars Numbers

✍个人博客:https://blog.csdn.net/Newin2020?spm=1011.2415.3001.5343
📚专栏地址:PAT题解集合
📝原题地址:题目详情 - 1100 Mars Numbers (pintia.cn)
🔑中文翻译:火星数字
📣专栏定位:为想考甲级PAT的小伙伴整理常考算法题解,祝大家都能取得满分!
❤️如果有收获的话,欢迎点赞👍收藏📁,您的支持就是我创作的最大动力💪

1100 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 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 进制来计数:

  • zero(零)在火星读作 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。

我们要做的就是转换火星数字与正常数字。

思路
  1. 处理正常数字
    1. 如果数字小于 13 ,则直接查找数组输出对应的值。
    2. 如果数字大于等于 13 ,则需要将数字划分成高低两位,这里需要注意的是,如果该数能被 13 除尽的话是只输出一个字符串的,例如 13 输出 tam 而不是 tam tret ,当有高位存在时低位如果为 0 则不进行输出。当然如果数字等于 0 的话,直接输出 tret 即可。
  2. 处理火星数字
    1. 如果给定的数字在高位数组中,则需要将数字转换成对应的正常数字并乘以 13 返回。
    2. 如果给定的数字在低位数组中,则直接查询并返回对应的数值即可。
代码
#include<bits/stdc++.h>
using namespace std;

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

int get(string str)
{
    //判断是否在高位
    for (int i = 1; i <= 12; i++)
        if (str == high[i])
            return i * 13;
    //判断是否在低位
    for (int i = 0; i <= 12; i++)
        if (str == low[i])
            return i;
}

void change(string num)
{
    if (num[0] >= '0' && num[0] <= '9')    //处理正常数字
    {
        int ans = stoi(num);
        if (ans < 13)  cout << low[ans] << endl;
        else
        {
            cout << high[ans / 13];
            if (ans % 13)   cout << " " << low[ans % 13];
            cout << endl;
        }
    }
    else    //处理火星数字
    {
        stringstream ssin(num);
        string str;
        int ans = 0;
        while (ssin >> str) ans += get(str);
        cout << ans << endl;
    }
}

int main()
{
    int n;
    cin >> n;
    getchar();

    for (int i = 0; i < n; i++)
    {
        string num;
        getline(cin, num);
        change(num);
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值