PAT乙组1044.火星数字思路与注意点--补充《算法笔记》

B1044

题目链接

个人思路

个人思路很麻烦,而且代码复杂,虽然AC,但编程比较耗时,且易出错

  • 数据结构:
    • 4个map,地球->火星(高位,低位);火星->地球(高位,低位)
    • 2个string散列,存高低位
    • 1个栈,地球->火星进制转换是从低位开始,而输出是从高位开始
  • 思路
    • 输入的是数字,判断有无进位并分别处理
    • 输入的是字符串,判断字符串个数来确定有无进位分别处理

个人思路代码

#include <bits/stdc++.h>
using namespace std;
string low[15] = {"tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};
string high[15] = {" ", "tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};
int N;
map<string, int> MtoE_low;
map<string, int> MtoE_high;
map<int, string> EtoM_low;
map<int, string> EtoM_high;
stack<string> mars;
int getNum(string s)
{
	int len = s.length();
	int ans = 0;
	for(int i = 0; i < len; ++i)
	{
		ans = ans * 10 + (s[i] - '0');
	}
	return ans;
}
int main(int argc, char *argv[]) {
	scanf("%d", &N);
	for(int i = 0; i < 13; ++i)
	{
		MtoE_low.insert(make_pair(low[i], i));
		MtoE_high.insert(make_pair(high[i], i));
		EtoM_low.insert(make_pair(i, low[i]));
		EtoM_high.insert(make_pair(i, high[i]));
	}
	getchar();
	while(N--)
	{
		string str;
		getline(cin, str);
		int len = str.length();
		if(str[0] >= '0' && str[0] <= '9')//输入的是数字 
		{	
			int num = getNum(str);
			if(num < 13)//无进位 
				cout << EtoM_low[num] << endl;
			else//含进位 
			{
				while(num)
				{
					int ret = num % 13;
					num /= 13; 
					len--;
					if(ret == 0)
						continue;
					if(num != 0)
						mars.push(EtoM_low[ret]);
					else
						mars.push(EtoM_high[ret]);
				}
				cout << mars.top();
				mars.pop();
				while(!mars.empty())
				{
					cout << " " << mars.top();
					mars.pop();
				}
				cout << endl;
			}
		}
		else//输入的是火星文 
		{
			if(len <= 3)//只有一串字符串 
			{
				string first = str.substr(0, 3);
				//判断是在高位还是低位
				if(MtoE_low.count(first) == 1)//是13以内的数字 
				{
					cout << MtoE_low[first] << endl;
				}
				else//产生进位 
				{
					cout << MtoE_high[first] * 13 << endl;
				}
			}
			else
			{
				string first = str.substr(0, 3);
				string second = str.substr(4, 3);
				int ans = MtoE_high[first] * 13 + MtoE_low[second];
				cout << ans << endl;
			}
		}
	}
	return 0;
}

本题思路

核心思想是打表,数据量较少,枚举0~169所有的数和其对应的火星字符串,根据输入直接访问输出即可

AC代码

#include <bits/stdc++.h>
using namespace std;
string unitNum[15] = {"tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};
string tenNum[15] = {"tret", "tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};
string E2M[170];//数字->火星文 
map<string, int> M2E;//火星文->数字 
int N;
void init()
{
	for(int i = 0; i < 13; i++)
	{
		E2M[i] = unitNum[i];
		M2E[unitNum[i]] = i;
		E2M[i * 13] = tenNum[i];
		M2E[tenNum[i]] = i * 13;
	}
	for(int i = 1; i < 13; i++)
	{
		for(int j = 1 ; j < 13; j++)
		{
			string mar = tenNum[i] + " " + unitNum[j];
			E2M[i * 13 + j] = mar;
			M2E[mar] = i * 13 + j;
		}
	}
}
int main(int argc, char *argv[]) {
	init();
	scanf("%d", &N);
	getchar();
	while(N--)
	{
		string str;
		int num = 0;
		getline(cin, str);
		if(str[0] >= '0' && str[0] <= '9')
		{
			for(int i = 0; i < str.length(); i++)
			{
				num = 10 * num + (str[i] - '0');
			}
			cout << E2M[num] << endl;
		}
		else
		{
			cout << M2E[str] << endl;
		}
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值