1100. Mars Numbers (20)

本文介绍了一个用于在地球数字系统和火星数字系统之间进行相互转换的程序。火星居民使用13进制计数,将地球上的0到12分别对应为tret到dec,并引入了新的高位数字名称。程序读取输入文件中的数字,这些数字可以是地球或火星形式,并输出相应的另一种形式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1100. Mars Numbers (20)
时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B
判题程序 Standard 作者 CHEN, Yue

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

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <queue>
#include <stack>
#include <map>
#include <set>

using namespace std;


void InitMap(map<string ,int>&lowb,map<string,int>&highb)
{
    lowb["tret"] = 0; lowb["jan"] = 1; lowb["feb"] = 2;
    lowb["mar"] = 3; lowb["apr"] = 4; lowb["may"] = 5;
    lowb["jun"] = 6; lowb["jly"] = 7; lowb["aug"] = 8;
    lowb["sep"] = 9; lowb["oct"] = 10; lowb["nov"] = 11;
    lowb["dec"] = 12;


    highb["tret"] = 0;
    highb["tam"] = 1; highb["hel"] = 2;highb["maa"] = 3;
    highb["huh"] = 4; highb["tou"] = 5;highb["kes"] = 6; 
    highb["hei"] = 7; highb["elo"] = 8;highb["syy"] = 9; 
    highb["lok"] = 10; highb["mer"] = 11;highb["jou"] = 12;
}

void InitString(string lowN[],string highN[])
{
    lowN[0] = "tret"; lowN[1] = "jan"; lowN[2] = "feb";
    lowN[3] = "mar"; lowN[4] = "apr"; lowN[5] = "may";
    lowN[6] = "jun"; lowN[7] = "jly"; lowN[8] = "aug";
    lowN[9] = "sep"; lowN[10] = "oct"; lowN[11] = "nov";
    lowN[12] = "dec";

    highN[0] = "tret";
    highN[1] = "tam"; highN[2] = "hel";highN[3] = "maa"; 
    highN[4] = "huh"; highN[5] = "tou";highN[6] = "kes"; 
    highN[7] = "hei"; highN[8] = "elo";highN[9] = "syy"; 
    highN[10] = "lok"; highN[11] = "mer";highN[12] = "jou";
}

enum TYPE {NUM,STR,NONE};

TYPE Judge(string str)
{
    if (str.size())
    {
        if ('0' <= str[0] && str[0] <= '9')
            return NUM;
        if ('a' <= str[0] && str[0] <= 'z')
            return STR;
    }
    return NONE;
}

void GetBit(string s, int & h, int & l)
{
    int num = 0;
    for (int i = 0; i < s.size(); ++i)
    {
        num = 10 * num + s[i] - '0';
    }


    h = num / 13;
    l = num % 13;
}

void GetStrb(string s, string &hb, string & lb, map<string,int>highb)
{
    hb.clear(), lb.clear();

    int pos = s.find(' ');

    if (pos != string::npos)
    {
        for (int i = 0; i < pos; ++i)
            hb += s[i];
        for (int i = pos + 1; i < s.size(); ++i)
            lb += s[i];
        return;
    }

    if (highb.count(s))
    {
        hb = s;
        return;
    }

    lb = s;
}

int ToBase13(int h, int l)
{
    return h * 13 + l;
}

int main()
{
#ifdef _DEBUG
    freopen("data.txt", "r+", stdin);
#endif // _DEBUG

    int n;
    map<string, int>lowb, highb;
    string lowStr[13], highStr[13],in;
    string hs, ls;
    int h, l;

    InitMap(lowb, highb);
    InitString(lowStr, highStr);

    cin >> n; getchar();

    while (n--)
    {
        getline(cin, in);
        TYPE type = Judge(in);

        switch (type)
        {
        case NUM:

            GetBit(in, h, l);
            if (h > 0)
                printf("%s", highStr[h].c_str());
            if (h && l)
                printf(" ");
            if(l > 0)
                printf("%s", lowStr[l].c_str());
            if (l || h)
                printf("\n");
            if (l == 0 && h == 0)
                printf("tret\n");
            break;
        case STR:
            GetStrb(in, hs, ls, highb);
            h = l = 0;
            if (hs.size())
                h =  highb[hs];
            if(ls.size())
                l = lowb[ls];
            printf("%d\n", ToBase13(h,l));
            break;
        case NONE:
            break;
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值