PTA(Advanced Level)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

思路

  • 要处理的就是2个映射的关系,即火星文->数字数字->火星文。前者可以用map<string, int>,后者可以用string a[n]
  • 可以手动模拟换算的过程也可以通过打表的方式,这里由于数据量比较小,打表会比较理想,代码量比较少

代码

#include<bits/stdc++.h>
using namespace std;
map<string, int> m;		// 火星文->数字
string ans[200];	// 数字->火星文
string digits[13] = {"tret", "jan", "feb", "mar", "apr", "may",
					 "jun", "jly", "aug", "sep", "oct", "nov",
					 "dec"};
string times_digits[13] = {"tret", "tam", "hel", "maa", "huh", "tou",
						 "kes", "hei", "elo", "syy", "lok", "mer",
						 "jou"};
void Init()
{
	for(int i=0;i<13;i++)
	{
		ans[i] = digits[i];
		m[digits[i]] = i;
		ans[i * 13] = times_digits[i];
		m[times_digits[i]] = i * 13;
	}

	for(int i=1;i<13;i++)
		for(int j=1;j<13;j++)
		{
			string tmp = times_digits[i] + " " + digits[j];
			ans[i * 13 + j] = tmp;
			m[tmp] = i * 13 + j;
		}	//手动模拟打表

}
int string2int(string s)
{
	int value = 0;
	for(int i=0;i<s.size();i++)
		value = value * 10 + (s[i] - '0');
	return value;
}	//将字符串->数字
int main()
{
	int n;
	string s;
	cin >> n;
	getchar();		// 处理换行符
	Init();		// 初始化打表过程

	for(int i=0;i<n;i++)
	{
		getline(cin, s);
		if(isdigit(s[0]))	// 数字
		{
			int value = string2int(s);
			cout << ans[value] << endl;
		}else
		{
			cout << m[s] << endl;
		}
	}
	return 0;
}

引用

https://pintia.cn/problem-sets/994805342720868352/problems/994805367156883456

原文地址:https://www.cnblogs.com/MartinLwx/p/12810223.html