您的位置:首页 > 其它

PAT - 甲级 - 1100. Mars Numbers (20)(字符串处理)

2017-03-30 23:48 375 查看
题目描述:

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


题目思路:

进行十三进制和十进制的互相转换,十进制的常规意义上的,十三进制是题目重新规定的,给出了高位和低位的表示方法。

1.13的倍数可以直接用1个高位“火星文”表示,不需要在低位补“tret”即"0"。

2.当输入的“火星文”只有一个单词的时候,需要判断是高位还是低位。

题目代码:

#include <cstdio>
#include <string>
#include <map>
#include <iostream>
using namespace std;

map<string, int>m1; // 低位
map<string, int>m2; // 高位
map<string, int>m3; // 低位+高位

map<int, string>n1; // 低位
map<int, string>n2; // 高位
map<int, string>n3; // 低位+高位
int num1[13] = {0,1,2,3,4,5,6,7,8,9,10,11,12};
int num2[26] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25};
string str1[13] = {"tret","jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};
string str2[13] = {"tret","tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};
string str3[26] = {"tret","jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec",
"tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};
string s1;
int n;
int main(){
scanf("%d",&n);
// 初始化 高 低位
for(int i = 0; i < 13; i++){
m1[str1[i]] = num1[i];
m2[str2[i]] = num1[i];
n1[num1[i]] = str1[i];
n2[num1[i]] = str2[i];
}
// 初始化 全
for(int i = 0; i < 26; i++){
m3[str3[i]] = num2[i];
n3[num2[i]] = str3[i];
}
getchar();
for(int i = 0; i < n; i++){
getline(cin,s1);
//数字
if(s1[0] >= '0' && s1[0] <= '9'){
int temp;
sscanf(s1.c_str(),"%d",&temp);
if(temp < 13)
cout<<n1[temp]<<endl;
else if(temp % 13 == 0)
cout<<n2[temp/13]<<endl;
else
cout<<n2[temp/13]<<" "<<n1[temp%13]<<endl;
}
// 字符串
else{
if(s1.length() == 3)
if(m3[s1] >= 13)
cout<<(m3[s1]-12) * 13<<endl;
else
cout<<m3[s1]<<endl;
else if(s1.length() == 4)
cout<<0<<endl;
else{
cout<<(m2[s1.substr(0,3)])*13+m1[s1.substr(4,3)]<<endl;
}
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: