PAT (Advanced Level)-1100. Mars Numbers (20)

PAT (Advanced Level)-1100. Mars Numbers (20)

1100. Mars Numbers (20)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue


People on Mars count their numbers with base 13:

Zero on Earth is called “tret” on Mars.
The numbers 1 to 12 on Earch 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进制。麻烦之处在于,mars数字需要用字符串来表示,所以应该做好转化进制时的数字和字符串的对应。
另外就是输出格式 火星数的整两位数,后面没有空格,所以对于整两位数和其他两位数需要区分处理。

完整代码

#include<iostream>
#include<string>
#include<sstream>
#include<cstring>
#include<vector>

// [mars_base] records the 0-12 on the mars
std::string mars_base[] = { "tret","jan","feb","mar","apr","may","jun","jly","aug","sep","oct","nov","dec" };
// [mars_higher] records the next higher digit on the mars
std::string mars_higher[] = { "tam","hel","maa","huh","tou","kes","hei","elo","syy","lok","mer","jou" };
//using namespace std;

std::string earth_to_mars(int x)
{
	if (x / 13 == 0)// 0-12
	{
		return mars_base[x];
	}
	else if (x / 13 != 0 && x%13 == 0)// 13 26 39 ....146 except 0, because 0 is in the [mars_base]
	{
		return mars_higher[x / 13 - 1];
	}
	else// the rest of double_digit which uses " "(space)
	{
		return mars_higher[x / 13 - 1] + " " + mars_base[x % 13];
	}
}

int mars_to_earth(std::string x)
{
	int num_0 = x.find(" ");// find out whether there is a " "(space) in the string 
	if (num_0 == std::string::npos)
	{
		for (int i = 0; i < 13; i++)
		{
			if (mars_base[i] == x)
			{
				return i;
			}
			else if (mars_higher[i] == x)
			{
				return (i + 1) * 13;
			}
		}
	}
	else
	{
		// to split the string in two with " "(space)
		std::string str1, str2;
		std::istringstream is(x);
		is >> str1 >> str2;

		int high=0, base=0;
		for (; high < 12; high++)
		{
			if (str1 == mars_higher[high])
				break;
		}
		for (; base < 13; base++)
		{
			if (str2 == mars_base[base])
			{
				break;
			}
		}
		return (high + 1) * 13 + base;
	}
}

int main()
{
	int N;
	std::cin >> N;
	getchar();// handle the ENTER in the cin after you input N
	std::vector<std::string> input(N);
	for (int i = 0; i < N; i++)
	{
		getline(std::cin,input[i]);
	}

	for (int j = 0; j < N; j++) 
	{
		if (input[j][0] <= '9' && input[j][0] >= '0') // earth
		{  
			std::cout << earth_to_mars(std::stoi(input[j])) << std::endl;
		}
		else // mars
		{
			std::cout << mars_to_earth(input[j]) << std::endl;
		}
	}
	return 0;
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注