PTA 乙级——1044 火星数字 C++实现

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_45677518/article/details/102749547

1044 火星数字

火星人是以 13 进制计数的:

  • 地球人的 0 被火星人称为 tret。
  • 地球人数字 1 到 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。

例如地球人的数字 29 翻译成火星文就是 hel mar;而火星文 elo nov 对应地球数字 115。为了方便交流,请你编写程序实现地球和火星数字之间的互译。

输入格式:
输入第一行给出一个正整数 N(<100),随后 N 行,每行给出一个 [0, 169) 区间内的数字 —— 或者是地球文,或者是火星文。

输出格式:
对应输入的每一行,在一行中输出翻译后的另一种语言的数字。

输入样例:

4
29
5
elo nov
tam

输出样例:

hel mar
may
115
13

代码

这个题主要问题容易处在这个火星文和地球文的相互转化以及他的输出格式规范。

火星文转地球文我是直接用的长度进行判断,地球文转火星文就先找到十位(/13)再找个位(%13)。

需要注意的是地球人的0是在火星文里面是tret,但是火星文的13、26里个位的0是不用打印的。

同理,火星文的长度为3时也有可能是13的整数倍。

//#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <map>
#include <sstream>

using namespace std;

string unit[] = { "tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec" };   //  个位
string tens[] = { "","tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou" };        //  十位


map <string, int> mUnit;
map <string, int> mTens;


void MartoEar(string s)  //  火星文转地球文
{
	if (s.length() == 4)
		cout << 0;
	if (s.length() == 3)
	{
		if (mUnit[s] != 0)
			cout << mUnit[s];
		else
			cout << mTens[s]*13;
	}
	if (s.length() == 7)
	{
		int value;
		string gewei,shiwei;
		shiwei = s.substr(0, 3);
		gewei = s.substr(4, 3);
		value = mTens[shiwei]*13 + mUnit[gewei];
		cout << value;
	}
    cout << endl;

}

void EartoMar(string s)  //  地球文转火星文
{
	int a = atoi(s.c_str());
	if (a / 13)
		cout << tens[a / 13];
	if (a % 13 != 0)
	{
		if (a > 13)
			cout << " ";
		cout << unit[a % 13];
	}
    if (a == 0)
		cout << unit[a];
    cout << endl;
}

int main()
{

	int n;
	scanf("%d", &n);
	getchar();    //  缓冲区中的n

	for (int i = 0; i < 13; i++)
	{
		mUnit[unit[i]] = i;
		mTens[tens[i]] = i;
	}


	
	string s;
	for (int i = 0; i < n; i++)
	{
		getline(cin, s);
		if (s[0] >= '0'&&s[0] <= '9')   //  是地球文
			EartoMar(s);
		else                            //  是火星文
			MartoEar(s);
	}




}

猜你喜欢

转载自blog.csdn.net/qq_45677518/article/details/102749547