1100 Mars Numbers (20分)

1 题目

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

2 解析

2.1 题意

输入地球数字(或火星数字)转换为火星数字或地球数字

2.2 思路

考虑到数字范围不是太大,用打表的方法。
用map,打印0~168范围内所用火星数字和地球数字对应关系。
对查询直接输出即可。

打表:

  • 首先考虑十位为0、个位为[0,12]的数字和个位为0、十位为[0,12]的数字:
    • 十位为0、个位为[0,12]的数字与字符串的映射就是“tret,jan, feb, mar, apr, may, jun, jly, aug, sep, oct, nov, dec”,直接建立[0,12]与字符串的映射即可;
    • 个位为0,十位为[0,12]的数字(它们是13的倍数)与字符串的映射就是“tret, tam, hel, maa, huh, tou, kes, hei, elo, syy, lok, mer, jou”,直接建立映射即可;
  • 然后,考虑十位和个位不为0的情况:
    • 直接拼接火星文,中间加上空格,建立[14,168]数字与字符串的映射;

对于一行的输入用getline(cin,str);,前面加上一个scanf("%*c")或getchar(),吸收换行符。

3 参考代码

#include <iostream>
#include <string>
#include <map>

using std::cin; using std::cout;
using std::endl; using std::string;
using std::map;

map<string, int> strToNum;
map<int, string> numToStr;

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

void Init(){
    for (int i = 0; i < 13; ++i)
    {
        strToNum[unitDigit[i]] = i;//十位为0的
        numToStr[i] = unitDigit[i];

        strToNum[tenDigit[i]] = i * 13;//个位为0的
        numToStr[i * 13] = tenDigit[i];
    }

    for (int i = 1; i < 13; ++i)//十位和个位非零的
    {
        for (int j = 1; j < 13; ++j)
        {
            string str = tenDigit[i] + " " + unitDigit[j];
            numToStr[i * 13 + j] = str;
            strToNum[str] = i * 13 + j;
        }
    }

}




int main(int argc, char const *argv[])
{
    Init();

    int n;
    scanf("%d%*c", &n);//输入查询个数,同时吸收换行符
    
    string str;
    for (int i = 0; i < n; ++i)
    {
        getline(cin, str);
        if(str[0] >= '0' & str[0] <= '9'){
            int num = 0;
            for (int j = 0; j < str.length(); ++j)
            {
                num = num * 10 + (str[j] - '0'); 
            }
            cout << numToStr[num] <<endl;
        }else{
            printf("%d\n", strToNum[str]);
        }
    }

    return 0;
}
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 岁月 设计师:pinMode 返回首页