PAT_B_1044 Digital Mars

Title Description

Mars is 13 hexadecimal counting: 

Earth 0 is called Martian tret. 
Digital Earth Martian 1-12 were: jan, feb, mar, apr , may, jun, jly, aug, sep, oct, nov, dec. 
Martian would later carry 12 high figures were called: tam, hel, maa, huh , tou, kes, hei, elo, syy, lok, mer, jou. 
For example, the earth's digital translated into 29 Martian is hel mar; and Martian elo nov 115 correspond Digital Earth. In order to facilitate the exchange, please write programs to translate between the Earth and Mars numbers. 

Input format: 
given a positive input of the first line integer N (<100), followed by N rows, each row gives a [0, 169) within the digital section - Earth or text, the text or Mars. 

Output format: 
corresponding to each row of the input digital another language translation outputting in a row. 

Sample input: 
. 4 
29 
. 5 
Elo-Nov 
TAM 
Output Sample: 
HEL-Mar 
On May 
115 
13 is

AC Code

// PAT_1044_Mars
#include <ctype.h>
#include <stdio.h>
#include <string.h>

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

int Mars2Earth(char *s)
{
    if(s)
    {
        for(int i = 0; i < 13; i++)         
            if(strcmp(s, units[i]) == 0)
                return i;
        for(int i = 1; i < 13; i++)         
            if(strcmp(s, tens[i - 1]) == 0)
                return i * 13;
    }
    return 0;
}

int main()
{
    int N, m;
    char line[11];

    fgets(line, 11, stdin);
    sscanf(line, "%d", &N);
    for(int i = 0; i < N; i++)
    {
        fgets(line, 11, stdin);
        if(isdigit(line[0]))                
        {
            sscanf(line, "%d", &m);
            if(m / 13 && m % 13)
                printf("%s %s\n", tens[m / 13 - 1], units[m % 13]);
            if(m / 13 && m % 13 == 0)
                printf("%s\n", tens[m / 13 - 1]);
            if(m / 13 == 0)
                printf("%s\n", units[m % 13]);
        }
        if(isalpha(line[0]))                  
        {
            m = Mars2Earth(strtok(line, " \n"));        
            m += Mars2Earth(strtok(NULL, " \n"));       
            printf("%d\n", m);
        }
    }

    return 0;
}

 

RR

Guess you like

Origin www.cnblogs.com/Robin5/p/11310320.html