PAT A1100: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 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的整数,直接对应low数组
    • 大于13且能被13整除的整数,直接对应high数组
    • 大于13且不能被13整除的整数,商对应high数组,余数对应low数组
  • 当输入的字符串:
    • 无空格字符,则表示要么是小于13的整数,要么是大于13且能被13整除的整数;
    • 有空格字符,先转换空格前的字符,再转换后面的字符
代码实现
#includecstdio
#includecstring
#includestring
#includeiostream
using namespace std;
char low[13][5]={"tret","jan","feb","mar","apr","may","jun","jly","aug","sep","oct","nov","dec"};
char high[13][5]={"","tam","hel","maa","huh","tou","kes","hei","elo","syy","lok","mer","jou"};
int N;
int changetonum(char str[]){
	for(int i=0;i13;i++)
		if(strcmp(low[i],str)==0) return i;
	for(int i=1;i13;i++)
		if(strcmp(high[i],str)==0) return i*13;
}
void solve()
{
    scanf("%d",N);
	getchar();
	for(int i=0;iN;i++){
        string s;
		getline(cin,s);
		if(s[0]='0's[0]='9'){  //地球数字 
			int sum=0;
			for(int j=0;js.size();j++)
				sum=10*sum+(s[j]-'0');
			if(sum13) printf("%s\n",low[sum]);
			else if(sum%13==0) printf("%s\n",high[sum/13]);
			else if(sum%13!=0) printf("%s %s\n",high[sum/13],low[sum%13]);
		}else{  //火星数字 
			char str[6];
			if(s.size()7){
				strcpy(str,s.c_str());
				printf("%d\n",changetonum(str));
			}else{
				string temp=s.substr(0,3);
				strcpy(str,temp.c_str());
				int sum=changetonum(str);
				temp=s.substr(4,3);
				strcpy(str,temp.c_str());
				printf("%d\n",sum+changetonum(str));
			}
		}
	}
}
int main(){
	solve();
    return 0;
}
最新回复(0)
/jishu5hPfFcAXSqxEr6TeC8o0VHf_2Fwjms6XJEiUN4lg_3D_3D4794644
8 简首页