码迷,mamicode.com
首页 > 其他好文 > 详细

1100. Mars Numbers (20)【字符串处理】——PAT (Advanced Level) Practise

时间:2016-05-18 19:28:45      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:

题目信息

1100. Mars Numbers (20)

时间限制400 ms
内存限制65536 kB
代码长度限制16000 B
People on Mars count their numbers with base 13:

Zero on Earth is called “tret” on Mars.
The numbers 1 to 12 on Earch 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

解题思路

字符串处理,细心即可

AC代码

#include <cstdio>
#include <string>
#include <cstring>
#include <map>
using namespace std;
char str1[13][5] = {"tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};
char str2[13][5] = {"tret", "tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};
int main()
{
    map<int, string> iskv1, iskv2;
    map<string, int> sikv1, sikv2;
    for (int i = 0; i <= 12; ++i){
        iskv1[i] = str1[i];
        iskv2[i] = str2[i];
        sikv1[str1[i]] = i;
        sikv2[str2[i]] = i;
    }
    int n, a;
    char s[1000], s2[5];
    scanf("%d", &n);
    gets(s);
    for (int i = 0; i < n; ++i){
        gets(s);
        if (s[0] >= ‘0‘ && s[0] <= ‘9‘){
            sscanf(s, "%d", &a);
            if (a > 12) {
                if (a%13 == 0) printf("%s\n", iskv2[a/13].c_str());
                else printf("%s %s\n", iskv2[a/13].c_str(), iskv1[a%13].c_str());
            }else{
                printf("%s\n", iskv1[a].c_str());
            }
        }else{
            char *p = strtok(s, " ");
            char *p2 = strtok(NULL, " ");
            if (p && p2) {
                a = sikv2[p] * 13;
                a += sikv1[p2];
            }else if (p){
                a = sikv2[p] * 13;
                a += sikv1[p];
            }
            printf("%d\n", a);
        }
    }
    return 0;
}

1100. Mars Numbers (20)【字符串处理】——PAT (Advanced Level) Practise

标签:

原文地址:http://blog.csdn.net/xianyun2009/article/details/51407894

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!