1100 Mars Numbers (数学题)
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
tam
Sample Output:
hel mar
may
115
13
结题思路
之前解答过本题,但是总是存在几个测试点通不过,此次抛弃先前的思路重新编写代码,发现代码越简洁越凝练,代码越容易被PAT OJ系统AC.(此为做题感悟),下面阐述本题结题思路,本题结题思路简单,应用到substr函数用于提取子串(分割),后续可以多留意使用
#include<iostream>
#include <cmath>
using namespace std;
string lowbit[]= {"tret","jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};
string highbit[]= {"","tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};
int find1(string s[],string tmp){
for(int i=0;i<13;i++){
if(tmp==s[i]) return i;
}
return -1;
}
int main(){
int n;
cin>>n;
getchar();
for(int i=0;i<n;i++){
string s;
getline(cin,s);
if(isdigit(s[0])){
int num=0;
for(int i=0;i<(int)s.size();i++){
num += (s[i]-'0')*pow(10,(int)s.size()-i-1);
}
if(num<13) cout<<lowbit[num]<<endl;
else{
cout<<highbit[num/13];
if(num%13) cout<<" "<<lowbit[num%13];
cout<<endl;
}
}else{
int spaceindex=0;
while(spaceindex<(int)s.size() && s[spaceindex]!=' ') spaceindex++;
if(spaceindex==(int)s.size()){
int pos1=find1(lowbit,s);
if(pos1!=-1) cout<<pos1<<endl;
else{
int pos2=find1(highbit,s);
cout<<pos2*13<<endl;
}
}else{
string s1=s.substr(0,spaceindex),s2=s.substr(spaceindex+1,(int)s.size()-1-(spaceindex+1)+1);
int pos1=find1(highbit,s1),pos2=find1(lowbit,s2);
cout<<(pos1*13+pos2)<<endl;
}
}
}
return 0;
}