1044 火星数字

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/piaoliangjinjin/article/details/81906933

火星人是以 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

分析;

// B44.cpp : Defines the entry point for the console application.
//

#include <iostream>
#include <string>
#include <cmath>
using namespace std;

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

//数字转换火星文 
void tras1(string s)
{
	int num = 0;
	int x = 0;
	for (int i = s.size() - 1; i >= 0; i--)
	{
		num += (s[i] - 48) * pow(10, x);
		x++;
	}
	if (num<13)
	{
		cout << num1[num] << endl;
	}
	else if (num % 13 == 0)
	{
		cout << num2[num / 13] << endl;
	}
	else
	{
		cout << num2[num / 13] << " " << num1[num % 13] << endl;
	}

}
//从火星文转换成数字
void tras2(string s)
{
	string a[13] = { "###", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec" };
	string b[13] = { "###", "tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou" };
	int len = s.length();
	if (len == 3)
	{
		for (int j = 1; j <= 12; j++)
		{
			if (s==a[j])
			{
				cout << j << endl;
			}
			else if (s == b[j])
			{
				cout << 13 * j << endl;
			}
		}
	}
	else
	{
		int t1 = 0;
		int t2 = 0;
		string s1 = s.substr(0, 3);
		string s2 = s.substr(4, 3);
		for (int j = 1; j <= 12; j++)
		{
			if (s1 == b[j])
			{
				t1 = j;
			}
			if (s2 == a[j])
			{
				t2 = j;
			}
		}
		cout << t1 * 13 + t2 << endl;
	}

}



int main()
{
	int n;
	scanf("%d\n", &n);

	string input;

	while (n--)
	{
		getline(cin, input);

		if (input[0] >= '0'&&input[0] <= '9')
		{
			tras1(input);
		}
		else
		{
			tras2(input);
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/piaoliangjinjin/article/details/81906933