1.题目考察十进制与13进制之间的转换。
2.注意输入为13,26,39整数时,只输出一个marsNum,后面低位的0不输出。
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代码:
[c language=”++”]
//#include<string>
//#include <iomanip>
#include<vector>
#include <algorithm>
//#include<stack>
#include<set>
#include<queue>
#include<map>
//#include<unordered_set>
#include<unordered_map>
//#include <sstream>
//#include "func.h"
//#include <list>
#include<stdio.h>
#include<iostream>
#include<string>
#include<memory.h>
#include<limits.h>
using namespace std;
bool isNum(string a)
{
for (int i = 0; i < a.size(); i++)
{
if (a[i]>’9′ || a[i] < ‘0’)
return false;
}
return true;
}
int main(void)
{
vector<string> marsNum = { "tret","jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec" };
vector<string> marsNum2 = { "tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou" };
map<string, int> mars2Num;
map<string, int> mars2Num2;
for (int i = 0; i < marsNum.size(); i++)
{
mars2Num[marsNum[i]] = i;
}
for (int i = 0; i < marsNum2.size(); i++)
{
mars2Num2[marsNum2[i]] = i+1;
}
string str = "";
string n = "";
getline(cin, n);
int sum = 0;
for (int i = 0; i < n.size(); i++)
{
sum = sum * 10 + n[i] – ‘0’;
}
for (int k = 0; k < sum; k++)
{
getline(cin, str);
if (isNum(str))
{
int num = 0;
for (int i = 0; i < str.size(); i++)
{
num = num * 10 + str[i] – ‘0’;
}
string ans = "";
int low = num % 13;//计算高位和地位
int high = num / 13;
if (high == 0)
ans = marsNum[low];
else if (low == 0)//注意输入为13,26,39整数时,只输出一个marsNum,后面低位的0不输出
ans = marsNum2[high – 1] ;
else
ans = marsNum2[high – 1] + " " + marsNum[low];
cout << ans << endl;
}
else
{
int ans = 0;
if (str.size() == 3)
{
if (mars2Num.find(str) != mars2Num.end())
{
ans = mars2Num[str];
}
else
{//找不到,是在十位上
ans = mars2Num2[str]*13;
}
}
else
{
string high = str.substr(0, 3);
string low = str.substr(4);
ans = mars2Num2[high] * 13 + mars2Num[low];
}
cout << ans << endl;
}
}
return 0;
}
[/c]