1100 Mars Numbers (20point(s)) - C語言 PAT 甲級

1100 Mars Numbers (20point(s))

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

題目大意:

1044 火星數字 (20point(s))

設計思路:

1044 火星數字(C語言)

  • 輸入數據的處理利用庫函數簡化,分類輸出即可
編譯器:C (gcc)
#include <stdio.h>
#include <string.h>

int printmars(int number, char *units[], char *tens[]);
int printearth(char *num, char *units[], char *tens[]);
int marsearth(char *s, char *units[], char *tens[]);

int main()
{
    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 n, number;
    char num[11];
    int i;

    fgets(num, 11, stdin);
    sscanf(num, "%d", &n);
    for(i = 0; i < n; i++){
        fgets(num, 11, stdin);
        if(isdigit(num[0])){
            sscanf(num, "%d", &number);
            printmars(number, units, tens);
        }
        else{
            printearth(num, units, tens);
        }
    }

    return 0;
}

int printmars(int m, char *units[], char *tens[])
{
    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]);
    }
    return 0;
}
int printearth(char *num, char *units[], char *tens[])
{
    int m;
    m = marsearth(strtok(num, " \n"), units, tens);
    m += marsearth(strtok(NULL, " \n"), units, tens);
    printf("%d\n", m);
    return 0;
}

int marsearth(char *s, char *units[], char *tens[])
{
    int i;
    if(s){
        for(i = 0; i < 13; i++){
            if(strcmp(s, units[i]) == 0){
                return i;
            }
        }
        for(i = 1; i < 13; i++){
            if(strcmp(s, tens[i -1]) == 0){
                return i * 13;
            }
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章