| 题目链接 (与PAT. A1100同题)
| 题目分析
进制转换 + 字符串处理
| 解题思路
1、由于输入的不确定性,直接使用getline(cin, str)
读入一整行
2、先打表,用 map<string, int>
表示进制转换关系(map
中string
可以带空格)
Tips:题目没有明说,对于Mars,当高位有值,低位为0时,只输出高位
如: 26
应该表示为 hel
而不是 hel tret
| AC程序(C++)
/**************************
//@Author: 3stone
//@ACM: PAT. A1100 Mars Numbers
//@Time: 17/3/5
//@IDE: VS2017
***************************/
#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<map>
using namespace std;
string low[14] = {"tret","jan","feb","mar","apr","may","jun","jly","aug","sep","oct","nov","dec"};
string high[14] = {"tret","tam","hel","maa","huh","tou","kes","hei","elo","syy","lok","mer","jou"};
string etom[300];
map<string, int> mtoe;
int main(){
int n;
string str;
for(int i = 0; i < 13; i++){//初始化 数制转换关系
mtoe[low[i]] = i;
mtoe[high[i]] = i * 13;
etom[i] = low[i];
etom[i * 13] = high[i];
}
//组成火星两位数的map映射
for(int i = 1; i < 13; i++)
for(int j = 1; j < 13; j++){ //个位
string str = high[i] + " " + low[j];
etom[i * 13 + j] = str;
mtoe[str] = i * 13 + j;
}
scanf("%d%*c", &n);
for(int i = 0; i < n; i++){
int num = 0;
getline(cin, str);
//数字
if(str[0]>='0' && str[0] <= '9'){
for(int j = 0; j < str.length();j++) //字符串转数字
num = num * 10 + (str[j] - '0');
printf("%s\n", etom[num].c_str());
}
//字符串(火星数字)
else{
cout << mtoe[str] << endl; //map直接输出
}
}
return 0;
}