PAT A1100 Mars Numbers (20分)

题目链接https://pintia.cn/problem-sets/994805342720868352/problems/994805367156883456

题目描述
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.

输入
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.

输出
For each number, print in a line the corresponding number in the other language.

样例输入
4
29
5
elo nov
tam

样例输出
hel mar
may
115
13

代码

#include <iostream>
#include <cstdio>
#include <string>
#include <map>
#include <algorithm>

using namespace std;

int main() {
	map<string, int> mp1, mp2;
	mp1["tret"] = 0;
	mp1["jan"] = 1;
	mp1["feb"] = 2;
	mp1["mar"] = 3;
	mp1["apr"] = 4;
	mp1["may"] = 5;
	mp1["jun"] = 6;
	mp1["jly"] = 7;
	mp1["aug"] = 8;
	mp1["sep"] = 9;
	mp1["oct"] = 10;
	mp1["nov"] = 11;
	mp1["dec"] = 12;
	mp2["tam"] = 1;
	mp2["hel"] = 2;
	mp2["maa"] = 3;
	mp2["huh"] = 4;
	mp2["tou"] = 5;
	mp2["kes"] = 6;
	mp2["hei"] = 7;
	mp2["elo"] = 8;
	mp2["syy"] = 9;
	mp2["lok"] = 10;
	mp2["mer"] = 11;
	mp2["jou"] = 12;

	string str1[15] = {"tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};
	string str2[15] = {"", "tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};

	int n;
	string temp;
	scanf("%d", &n);	
	getchar();
	for(int i = 0; i < n; i++ ){
		getline(cin, temp);
		int len = temp.size();
		if(temp[0] >= '0' && temp[0] <= '9') {
			int a = 0;
			for(int i = 0; i < len; i++)
				a = a * 10 + (temp[i] - '0');
			int d1 = a % 13;		
			int d2 = a / 13;		
			if(d1 ==0 && d2 == 0)
				cout<<str1[0];
			if(d2)
				cout<<str2[d2];
			if(d1 && d2)
				cout<<' ';
			if(d1)
				cout<<str1[d1];
			cout<<endl;
		} 
		else{	
			int i = 0, b1 = 0, b2 = 0;
			string s1, s2;
			if(len > 4) {		        //如果有两位
				for(; temp[i] != ' '; i++)
					s2 += temp[i];
				b2 = mp2[s2];	
				i++;		
				for(; temp[i] != '\0'; i++)
					s1 += temp[i];
				b1 = max(mp1[s1], mp2[s1]);	//
			}
			else{												//只有一位
				for(; temp[i] != '\0'; i++)
					s1 += temp[i];
				if(mp1[s1] > mp2[s1])					       //是第一位
					b1 = max(mp1[s1], mp2[s1]);
				else											//是第二位
					b2 = max(mp1[s1], mp2[s1]);
			}
			int sum = b2 * 13 + b1;	
			printf("%d\n", sum);
			s1.clear();
			s2.clear();
		}	
	}
	return 0;
}
发布了364 篇原创文章 · 获赞 13 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/Rhao999/article/details/105251209