1100. Mars Numbers (20)字符串处理——PAT (Advanced Level) Practise
Posted 闲云阁
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1100. Mars Numbers (20)字符串处理——PAT (Advanced Level) Practise相关的知识,希望对你有一定的参考价值。
题目信息
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的主要内容,如果未能解决你的问题,请参考以下文章