PAT甲级 A1100
题目详情
1100 Mars Numbers (20分)
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
解题思路
用数组存储十位和百位的数字,getline进行行读入,判断是数字转字符还是字符转数字,需要特殊考虑输入为0或tret的情况
以下为AC代码
#include <cstdio>
#include<algorithm>
#include<iostream>
#include<vector>
#include<cmath>
#include<iomanip>
#include<map>
#include<queue>
#include<string>
using namespace std;
int N;
string lower[13] = { "0","jan","feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec" };
string higher[13] = { "0","tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou" };
int main() {
cin >> N;
string ept;
getline(cin, ept);
for (int i = 0; i < N; i++) {
string a;
getline(cin, a);
if (a.length() <= 3 && (a[0] >= '0' && a[0] <= '9')) {
int t = 1;
int sum = 0;
for (int j = a.length() - 1; j >= 0; j--) {
sum += (a[j]-'0') * t;
t = t * 10;
}
if (sum == 0) {
cout << "tret" << '\n';
continue;
}
if (sum / 13 != 0) {
cout << higher[sum/13];
}
if (sum % 13 != 0) {
if (sum > 13) {
cout << " ";
}
cout << lower[sum % 13];
}
cout << '\n';
}
else if (a.length() == 3) {
if (a == "tret") {
cout << 0 << '\n';
continue;
}
for (int j = 1; j <= 12; j++) {
if (a == lower[j]) {
cout << j << '\n';
break;
}
if (a == higher[j]) {
cout << 13 * j << '\n';
break;
}
}
}
else if(a.length()>3){
string hi = "";
hi += a[0];
hi += a[1];
hi += a[2];
string low = "";
low += a[4];
low += a[5];
low += a[6];
int sum=0;
for (int j = 1; j <= 12; j++) {
if (hi == higher[j]) {
sum = sum + j * 13;
}
if (low == lower[j]) {
sum = sum + j;
}
if (hi == lower[j]) {
sum = sum + j;
}
if (low == higher[j]) {
sum = sum + j*13;
}
}
cout << sum << '\n';
}
}
}