1100 Mars Numbers (20分)【string】

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进制计数的:地球人的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”。为了方便交流,请你编写程序实现地球和火星数字之间的互译。(其中给出的数字范围在[0,169)区间内)

解题思路:

 用两个string数组unitDigit和tenDigit来分别保存火星文,然后就是对数字进行10进制和13进制间的转换问题了。要注意:13的倍数不应当输出个位的“tret",例如13应当输出"tam"而不是"tam tret"。

#include<iostream>
#include<string>
#include<ctype.h>
using namespace std;

string unitDigit[13] = { "tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec" };    //个位数字
string tenDigit[12] = { "tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou" };    //进位位数字

int fun1(string s)      //火星文->地球文
{
	int num = 0;
	if (s.length() > 3) 
	{
		string a = s.substr(0, 3);
		string b = s.substr(4, 5);
		for (int i = 0; i < 12; i++)  //处理十位位
		{
			if (a == tenDigit[i])
			{
				num += (i + 1) * 13;
				break;
			}
		}
		for (int i = 0; i <= 12; i++)  //处理个位
		{
			if (b == unitDigit[i])
			{
				num += i;
				break;
			}
		}
	}
	else         //否则只需要处理一个十位或个位
	{
		for (int i = 0; i <= 12; i++)
		{
			if (s == unitDigit[i])
			{
				num = i;
				break;
			}
		}
		for (int i = 0; i < 12; i++)
		{
			if (s == tenDigit[i])
			{
				num = (i + 1) * 13;
				break;
			}
		}
	}
	return num;
}

string fun2(int num)               //地球文->火星文
{
	string s = "";
	if (num >= 13)
	{
		s += tenDigit[num / 13 - 1];
		if(num%13)
			s += " ";
	}
	if (num % 13 || num == 0)
		s += unitDigit[num % 13];
	return s;
}

int main()
{
	int n;
	cin >> n;
	getchar();
	for (int i = 0; i < n; i++)
	{
		string s;

		getline(cin, s);
		if (s[0] >= 'a'&&s[0] <= 'z')      //如果是字母
		{
			cout << fun1(s) << endl;
		}
		else
		{
			int num = 0;
			for (int j = 0; j < s.length(); j++)
			{
				num *= 10;
				num += (s[j] - '0');
			}
			cout << fun2(num) << endl;
		}
	}
}

 

posted @ 2020-04-21 20:19  Hu_YaYa  阅读(22)  评论(0)    收藏  举报