题目来源:PAT (Advanced Level) Practice
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
words:
count 计数,总数 communication 交流,通讯 mutual 相互的
题意:
火星文是十三进制的,给出火星文求对应的地球文或给出地球文求对应的火星文;
思路:
1. 将输入以字符串s的形式保存起来;
2. 判断字符串s的第一个字符是否为数字,若为数字则表示为地球文,则将其转化为火星文,否则便是为火星文,则将其转化为地球文;
3. 火星文的数字0是不需要输出的,无论在哪一位,除非这个火星文就是0;
//PAT ad 1100 Mars Numbers
#include <iostream>
using namespace std;
string mars1[12]={"tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};
string mars2[13]={"tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};
void zh1(string s) //将地球数字转化为火星文
{
int n=0,i;
for(i=0;i<s.size();i++) //求和
n=n*10+s[i]-'0';
int c2=n%13; //个位
int c1=n/13; //十位
string ans;
if(c1>0&&c2>0)
ans=mars1[c1-1]+" "+mars2[c2];
else if(c1>0)
ans=mars1[c1-1];
else
ans=mars2[c2];
cout<<ans<<endl;
}
void zh2(string s) //将火星文转化为地球数字
{
int a=0,b=0,i;
string s1,s2;
if(s.size()<5) //火星文由一位数构成
{
for(i=0;i<13;i++)
if(mars2[i]==s)
{
b=i; break;
}
if(i==13) //火星文只有十位数字
{
for(i=0;i<12;i++)
if(mars1[i]==s)
{
a=i; break;
}
cout<<(a+1)*13<<endl;
}
else //火星文只有个位数字
{
cout<<b<<endl;
}
}
else //火星文由二位数构成
{
for(i=0;s[i]!=' ';i++) //求和
s1.push_back(s[i]);
for(i++;i<s.size();i++) //求和
s2.push_back(s[i]);
for(i=0;i<12;i++)
if(mars1[i]==s1)
{
a=i; break;
}
for(i=0;i<13;i++)
if(mars2[i]==s2)
{
b=i; break;
}
cout<<(a+1)*13+b<<endl;
}
}
int main()
{
int n;
cin>>n;cin.ignore();
string s;
while(n--)
{
getline(cin,s); //输入
if(isdigit(s[0])) //是地球文
zh1(s); //地球文转化为火星文
else //是火星文
zh2(s); //火星文转化为地球文
}
return 0;
}