PAT乙级1044

题目链接

思路

1、判断输入是数字还是字符
2、如果是数字,先转为13进制,由于这里规定了数字不会超过168,那么这里可以直接通过两次
取余,然后去分别打印高位和低位对应的火星文。
3、是火星文的话就去相应的数组里找下标,这里将high这个数组补成了13个元素,也可以不补(在求出相应的下标后,先i-1,然后*13)
*注意的地方:转换为13进制后,诸如10,20,30... 这样数字的0不需要输出

实现


#include <iostream>
#include <cstdio>
#include <string>
#include <cmath>
#include <vector>
typedef long long ll;
using namespace std;
const string high[13] = { "nan", "tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok",
		"mer", "jou" };
const string low[13] = { "tret","jan", "feb", "mar", "apr", "may", "jun", "jly", "aug",
		"sep", "oct", "nov", "dec" };

void NumToMars(string str)
{
	int num = atoi(str.c_str());
	if (num < 13)
		cout << low[num] << endl;
	else
	{
		cout << high[num / 13];
		if (num % 13)
		{
			cout << " " << low[num % 13] << endl;
		}
		else
			cout << endl;
	}
}
void MarsToNum(string str)
{

	int result = 0,j,i;
	string strH="", strL="";
	if (str.size() > 3)
	{
		int pos = str.find_first_of(' ');
		strH = str.substr(0, pos);
		strL = str.substr(pos + 1, str.size() - pos-1);
		for (j = 0; j < 13; j++)
		{
			if (high[j] == strH)
				result += j * 13;
			if (low[j] == strL)
				result += j;
		}
	}
	else
	{
		strH = str;
		for (int i = 0; i < 13; i++)
		{
			if (strH == high[i])
			{
				result += i * 13;
				break;
			}
			if (strH == low[i])
			{
				result += i;
				break;
			}
		}
	}
	cout << result << endl;
}
int main()
{
	int N,i;
	string strs[100];
	scanf("%d",&N);
	getchar();
	for (i = 0; i < N; i++)
	{
		getline(cin, strs[i]);
	}
	for (i = 0; i < N; i++)
	{
		int j;
		if (isdigit(strs[i][0]))
		{
			NumToMars(strs[i]);
		}
		else
		{
			MarsToNum(strs[i]);
		}
	}
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值