- Mars Numbers (20)
时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
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
一道进制转换的模拟题。对于PAT的题来说,用string是可以的,而且string套路里的函数灵活方便,还当继续努力学习。
#include<iostream>
#include<string>
#include<cstring>
#include<cstdio>
using namespace std;
string mars[25] = { "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" };
int equelstr(string *mars, string str)
{
int i;
for (i = 0; i < 25 && mars[i] != str; i++);
return i;
}
int main()
{
string str,strt;
int N, num,temp,goal;
cin >> N;
getchar();//吸收掉N
while (N--)
{
getline(cin, str);
if (str[0] >= '0'&&str[0] <= '9' ) //判断翻译方向
{
//sscanf进行字符串截取,相当实用
sscanf(str.c_str(), "%d", &num); //区别于scanf,sscanf以字符串为输入源,而scanf以键盘输入为输入源
if (num < 13 || num>12 && num % 13 == 0)//进行13进制演导
cout << mars[num%13+(num>12?num/13+12:0)] << endl;
else
cout << mars[num / 13+12] << " " << mars[num % 13] << endl;
}
else
{
temp = str.find(' ');
if (temp != -1)
{
strt = str.substr(0,3); //复制从0开始长度为3的字符串,返回到字符串strt
goal = (equelstr(mars, strt)-12)*13;
strt = str.substr(temp+1,3);
goal += equelstr(mars, strt);
}
else
{
goal=equelstr(mars, str);
goal = goal > 12 ? (goal - 12) * 13 : goal;
}
cout << goal << endl;
}
}
return 0;
}