1100. Mars Numbers

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.

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进制,用一个长度3的字符串表示一个地球数字,火星数字最多只有两位,且高位和低位相同数字的表示方式不同,要求将给定的地球(火星)数字转换成对应的火星(地球)数字并输出。

思路

整体思路上就是13进制转换和数字表示方式转换,没有什么问题。关键在细节处理上有很多坑,主要有以下几点:

  • 输入的火星数字只有一位时,要判断是高位数字还是低位数字,低位直接转换输出,高位则要乘以13再输出;
  • 地球数字转换成火星数字时,如果存在高位且低位为0,则不输出低位,只输出高位。

代码实现

#include <iostream>
#include <string>
#include <map>
using namespace std;

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

void createMap()        // 映射创建
{
    for (int i = 0; i < 13; i++)
    {
        to_E_Higher[to_M_Higher[i]] = i;
        to_E_Lowwer[to_M_Lowwer[i]] = i;
    }
}

void solve(string s)
{
    if (s[0] >= '0' && s[0] <= '9')             // 输入为地球数字
    {
        int num = 0;
        for (int i = 0; i < s.length(); i++)
            num = num * 10 + s[i] - '0';        // 求具体数字
        int b = num % 13;                       // 低位
        int a = num / 13;                       // 高位
        if (a != 0)                             // 存在高位
            if (b != 0)                         // 低位不为0,高低位都要输出
                cout << to_M_Higher[a] << " " << to_M_Lowwer[b] << endl;
            else                                // 低位为0,只输出高位
                cout << to_M_Higher[a] << endl;
        else                                    // 只存在低位
            cout << to_M_Lowwer[b] << endl;
    }
    else if (s.length() == 7)                   // 输入为火星数字,且高低位都存在
    {
        string x = s.substr(0, 3);              // 字符串拆分
        string y = s.substr(4, 3);
        int a = to_E_Higher[x];
        int b = to_E_Lowwer[y];
        cout << a * 13 + b << endl;
    }
    else if (to_E_Higher.find(s) != to_E_Higher.end())      // 只输入高位火星数字
        cout << to_E_Higher[s] * 13 << endl;
    else                            // 输入低位火星数字
        cout << to_E_Lowwer[s] << endl;
}

int main()
{
    int n;
    string s;

    createMap();

    cin >> n;
    getchar();
    for (int i = 0; i < n; i++)
    {
        getline(cin, s);
        solve(s);
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值