[PAT (Basic Level) Practice] - [detailed explanation of common usage of map] 1044 Mars numbers

Posted by sriusa on Mon, 31 Jan 2022 04:10:24 +0100

I. [Topic difficulty]

  • Class B

II. [Title No.]

  • 1044 Mars number (20 points)

III. [Title Description]

  • Martians are counted in hexadecimal:
  • Earthman's 0 is called tret by Mars.
  • The Martian characters of earthman numbers 1 to 12 are: jan, feb, mar, apr, may, jun, jly, aug, sep, oct, nov, dec.
  • The Martians call the 12 high-order numbers after rounding: tam, hel, maa, huh, tou, kes, hei, elo, syy, lok, mer, jou.
  • For example, the number 29 of earthman is translated into Martian, which is hel mar; The Martian text elo nov corresponds to the earth number 115. In order to facilitate communication, please write a program to translate the numbers between earth and Mars.

IV. [Title example]

  • Input format:
    The first line of input gives a positive integer N ( < 100 ) N(<100) N (< 100), then N N N lines, each line gives a number in the [0, 169) range - either earthy or Martian.

  • Output format:
    Corresponding to each input line, the translated numbers of another language are output in one line.

  • Input sample:
    4
    29
    5
    elo nov
    tam

  • Output example:
    hel mar
    may
    115
    13

V. [problem solving ideas]

  • At first glance, this question doesn't have any ideas. In fact, when you think about it carefully, the overall idea is quite coherent, but some details need to be paid attention to. The specific ideas are listed below:
    ① : first define two maps to store from 0 to 12 (Martian text) before carry and from 0 to 12 (Huoxing text) after carry. The second map needs to leave the first one empty because there is no carry (earth ball text) and ensure the continuity of carry and operation after carry. Then read in each string in turn to prepare for the next step to judge whether it is earth text or Huoxing text
    ② : if it is earthly, it needs to be converted to Martian. First, convert the string into the corresponding number, and then output it in three cases. The first one: the current number is greater than 13 and is a multiple of 13; Second: the current number is greater than 13 and is not a multiple of 13; Third: the current number is less than 13 (Note: there can be no more one here to judge whether the current number takes the modulus of 13 is not 0, because there is 0, so there may be a case where the modulus of 13 is equal to 0, otherwise there will be a use case that cannot pass), and then judge the output in turn
    ③ : if it is Martian, it needs to be converted to earth. The first case: a Martian text only needs to be scanned once to encounter the corresponding output; The second case: for two Martian characters, you need to scan both sides and encounter the corresponding output. Note that the carry here is hexadecimal
    ④ Note: except for the last output, all the others need to wrap
    ⑤ Note that when scanf and gets are used together, getchar() needs to be used to absorb carriage returns

Vi. [final score]

  • 20 points

VII. [code implementation]

#include<stdio.h>
#include<string.h>
int main()
{
    char lowDig[13][5] = {"tret","jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};
    char highDig[13][4] = {"","tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};
    int n;
    char str[10];
    scanf("%d",&n);
    getchar();
    for(int i = 0;i<n;i++)
    {
        gets(str);
        int num = 0,len = strlen(str);
        if(str[0] >= '0' && str[0] <= '9')
        {
            for(int j = 0;j<len;j++)
            {
                num = num * 10 + (str[j] - '0');
            }
            if(num / 13 != 0 && num % 13 == 0)
            {
                printf("%s",highDig[num/13]);
            }
            else if(num / 13 != 0 && num % 13 != 0)
            {
                printf("%s %s",highDig[num / 13],lowDig[num % 13]);
            }
            else if(num / 13 == 0)
            {
                printf("%s",lowDig[num % 13]);
            }
        }
        else
        {
            if(len <= 4)
            {
                for(int i = 0;i<13;i++)
                {
                    if(strcmp(str,lowDig[i]) == 0)
                    {
                        num = i;
                        break;
                    }
                    else if(strcmp(str,highDig[i]) == 0)
                    {
                        num = i * 13;
                        break;
                    }
                }
            }
            else
            {
                for(int i = 0;i<13;i++)
                {
                    if(strncmp(str,highDig[i],3) == 0)
                    {
                        num += i * 13;
                    }
                    if(strcmp(&str[4],lowDig[i]) == 0)
                    {
                        num += i;
                    }
                }
            }
            printf("%d",num);
        }
        if(i < n - 1)
        {
            printf("\n");
        }
    }
    return 0;
}

VIII. [submission results]

Topics: C Algorithm data structure