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 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 tamSample Output:
hel mar may 115 13
https://www.patest.cn/contests/pat-a-practise/1100
思路:
对于E-M变换,分别“/”“%”,然后取对应的下标即可
对于M-E变换,用map映射
注意M形式的时候,省略个位的0
CODE:
#include<iostream>
#include<cstring>
#include<string>
#include<map>
#include<cstdio>
#include<cstdlib>
using namespace std;
string s1[13]={"tret","jan","feb","mar","apr","may","jun","jly","aug","sep","oct","nov","dec"};
string s2[13]={"tret","tam","hel","maa","huh","tou","kes","hei","elo","syy","lok","mer","jou"};
map<string,int> ma;
int main()
{
for (int i=0;i<13;i++)
{
ma[s1[i]]=i;
ma[s2[i]]=i*13;
}
int n;
cin>>n;
getchar();
for(int i=0;i<n;i++)
{
string a;
getline(cin,a);
if (a[0]>='0'&&a[0]<='9')
{
int t=atoi(a.c_str());
if (t/13!=0)
{
cout<<s2[t/13];
t%=13;
if (t!=0)
cout<<" "<<s1[t]<<endl;
else
cout<<endl;
}
else
cout<<s1[t]<<endl;
}
else
{
int pos=-1;
for (int i=0;i<a.length();i++)
{
if (a[i]==' ')
{
pos=i;
break;
}
}
int t=0;
if (pos==-1)
{
cout<<ma[a]<<endl;
}
else
{
t+=ma[a.substr(0,pos).c_str()];
t+=ma[a.substr(pos+1,a.length()-pos-1).c_str()];
cout<<t<<endl;
}
}
}
return 0;
}