PAT Class B 1044 Mars Numbers (Problem Solving Ideas + AC Codes)

topic:

Martians count in base 13:

  • Earthlings' 0 is called tret by Martians.
  • The Martian characters for the numbers 1 to 12 are: jan, feb, mar, apr, may, jun, jly, aug, sep, oct, nov, dec.
  • Martians call the 12 high digits after the carry: tam, hel, maa, huh, tou, kes, hei, elo, syy, lok, mer, jou.

For example, the numbers of people on the earth 29can be translated into Martian characters hel mar; and the Martian characters elo novcorrespond to the numbers of the earth 115. In order to facilitate communication, please write a program to realize the mutual translation between the numbers of the earth and Mars.

Input format:

Enter a positive integer N (<100) in the first line of input, and then N lines, each line gives a number in the interval [0, 169) - either Earth language or Martian language.

Output format:

For each line of input, output the translated number in another language on one line.

Input sample:

4
29
5
elo nov
tam

Sample output:

hel mar
may
115
13

Code length limit 16 KB

time limit 400 ms

Memory limit 64 MB

 

problem solving ideas

  • Define two string arrays, one for low-order Martian characters and one for high-order Martian characters. The subscripts of the array correspond to the corresponding translated Martian text.
  • When the input is a number, we need to first confirm that the number is converted to the high and low bits of the hexadecimal system, and then output two arrays with the high and low bits as the values ​​corresponding to the subscripts.
  • When the input is Martian text, we need to separate the high-order and low-order characters of the Martian text, and then compare them with the two arrays one by one to translate the numbers corresponding to the high-order and low-order Martian text.

 

AC code

#include <bits/stdc++.h>
using namespace std;

string low[13] = {
    
    "tret","jan", "feb", "mar", "apr", "may", "jun", "jly"
                  ,"aug", "sep", "oct", "nov", "dec"};

string high[13] = {
    
    "000","tam", "hel", "maa", "huh", "tou", "kes", "hei"
                   ,"elo", "syy", "lok", "mer", "jou"};
//数字转换火星文
void my_itoa(string tmp)
{
    
    
    int sum = 0;
    for (int i = 0; i < tmp.size(); i++)
    {
    
    
        sum = sum * 10 + (tmp[i] - '0');
    }
    int ge = sum % 13;
    int shi = sum / 13;
    if (shi != 0)
    {
    
    
        cout << high[shi];
        if (ge != 0)
        {
    
    
            cout << " " << low[ge];
        }
    }
    else
    {
    
    
        cout << low[ge];
    }
    cout << endl;
}
//火星文转换数字
void my_atoi(string tmp)
{
    
    
    string ge = "0", shi_or_ge = "0";
    int right = 0, left = 0;
    if (tmp.size() > 3)
    {
    
    
        ge = tmp.substr(4, 3);
    }
    shi_or_ge = tmp.substr(0,3);
    for (int i = 0; i < 13; i++)
    {
    
    
        if (low[i] == ge)
        {
    
    
            right = i;
        }
        if (low[i] == shi_or_ge)
        {
    
    
            right = i;
        }
        if (high[i] == shi_or_ge)
        {
    
    
            left = i;
        }
    }
    cout << left * 13 + right << endl;
}

int main()
{
    
    
    int n = 0;
    cin >> n;
    //注意,这里cin不会读取\n,所以输入缓冲区中还有一个\n,所以我们需要先把它给拿出来。
    cin.get();
    for (int i = 0; i < n; i++)
    {
    
    
        string tmp = "0";
        //getline会读取\n,并将其遗弃,所以这里就不用cin.get()丢弃了
        getline(cin, tmp);
        if (tmp[0] >= '0' && tmp[0] <= '9')
        {
    
    
            my_itoa(tmp);
        }
        else
        {
    
    
            my_atoi(tmp);
        }
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/m0_70103775/article/details/130986262