PAT甲级-1100-Mars Numbers(字符串)

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
一、地球文与火星文转换规则:

火星文采用了13进制的转换方式,以下给出了火星文低12,高12位的对应转换规则(加上0,是13位):

 1    2    3    4    5    6    7    8    9   10   11   12
jan, feb, mar, apr, may, jun, jly, aug, sep, oct, nov, dec(低12位) 
tam, hel, maa, huh, tou, kes, hei, elo, syy, lok, mer, jou (高12位)
其中地球上的0在火星上是tret

eg.
“hel mar” (mars)- - - 29(earth)

  • 29 / 13 = 2 - - - hel
  • 29 % 13 = 3 - - - mar

“elo nov” (mars)- - - 115(earth)

  • 115 / 13 = 8 - - - elo
  • 115 % 13 = 11 - - - nov
二、转换时可能出现的问题:

火星文转地球文时,由于不知道其是一位数还是两位数,if(s2 == l[i]||s1==l[i]) n += i;便很好地解决了这个问题,如果只有一位:便是s1=l[i];如果有两位,便是s2=l[i]。(其中s1是用户输入字符串的高三位,s2是低三位)。

代码如下

#include<iostream>
#include<cctype>
using namespace std;
string l[13]={"tret","jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};
string h[13]={"","tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};
string s;
void etom(int n){
	if(n / 13) cout<<h[n/13];
	if(n/13 && n%13) cout<<" ";
	if(n%13 || n==0) cout<<l[n%13];
}
void mtoe(){
	int n = 0;
	string s1 = s.substr(0,3),s2;
	if(s.length() > 4) s2 = s.substr(4,3);
	for(int i = 1; i < 13; i++){
		if(s1 == h[i]) n += 13*i;
		if(s2 == l[i]||s1==l[i]) n += i;
	}
	cout<<n;
}
int main()
{
	int n;
	scanf("%d\n", &n);
	for(int i = 0; i < n; i++){
		getline(cin, s);
		if(isdigit(s[0])){
			etom(stoi(s));
		}else{
			mtoe();
		}
		cout<<endl;
	}
	return 0;
}
发布了110 篇原创文章 · 获赞 746 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/qq_42437577/article/details/104172958