PAT 1100 Mars Numbers [map] [打表]

本文介绍了一种将火星上的十三进制计数系统与地球上常用的十进制计数系统进行相互转换的方法。通过创建映射表,实现从火星文到数字的转换,以及从数字到火星文的逆向转换。文章提供了两种解决方案:一种是通过打表法预先生成所有可能的转换,另一种是在运行时动态计算转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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+第二个字符串所在下标。

数字→火星文时,火星文的第一个字符串的下标 = 数字/13 ,第二个 = 数字%13。

注意,13的倍数无论是火星文还是数字都不要输出0。

//打表法

#include<cstdio>
#include<string>
#include<map>
#include<iostream>

using namespace std;

map<string,int> mp;
string lowD[13] = {"tret","jan","feb","mar","apr","may","jun","jly","aug","sep","oct","nov","dec"};
string highD[13] = {"","tam","hel","maa","huh","tou","kes","hei","elo","syy","lok","mer","jou"};

//169的范围很小可以直接打表 
void Init()
{	
	int i;
	//小于13无需组合 
	for(i=0;i<13;i++)
		mp[lowD[i]] = i;
	for(i=13;i<169;i++)
	{
		string str;
		//13的倍数不需要第二个字符串 
		if(i%13==0) str = highD[i/13];
		//记得中间加" " 
		else str = highD[i/13]+" "+lowD[i%13];
		mp[str] = i;	
	}	
}

int main()
{
	int n,i;
	cin>>n;
	Init();
	getchar();
	while(n--)
	{
		string s;
		//一行一行读取,因为火星文中间有空格 
		getline(cin,s);
		string newS;
		//如果读入的是数字 
		if(s[0]<='9'&&s[0]>='0')
		{
			int r = 0;
			//先转换为整型 
			for(int i=0;i<s.size();i++)
				r = r*10 + s[i] - '0';
			//13的倍数(判断条件得加上r!=0不然会误伤) 
			if(r!=0&&r%13==0) newS = highD[r/13];
			else if(r>13) newS = highD[r/13]+" "+lowD[r%13];
			else newS = lowD[r];
			cout<<newS<<endl;	
		}
		else cout<<mp[s]<<endl;
	}
	return 0;
}
//不打表法

#include<cstdio>
#include<string>
#include<cctype>
#include<map>
#include<algorithm>
#include<iostream>

using namespace std;

int main()
{
	int n,i;
	string ten[15] = {"","tam","hel","maa","huh","tou","kes","hei","elo","syy","lok","mer","jou"};
	string one[15] = {"tret","jan","feb","mar","apr","may","jun","jly","aug","sep","oct","nov","dec"};
	string s;
	map<string,int> mp1,mp2;
	for(i=0;i<13;i++)
		mp1[ten[i]] = i;
	for(i=0;i<13;i++)
		mp2[one[i]] = i;
	cin>>n;
	getchar();
	for(i=0;i<n;i++)
	{
		getline(cin,s);
		if(isdigit(s[0]))
		{
			int tmp = stoi(s);
			//cout<<tmp<<endl;
			if(tmp%13==0&&tmp!=0) cout<<ten[tmp/13]<<endl;
			else if(tmp<13) cout<<one[tmp]<<endl;
			else cout<<ten[tmp/13]<<" "<<one[tmp%13]<<endl;;
		}
		else
		{
			int pos = s.find(" ");
			if(pos!=string::npos)
			{
				string s2 = s.substr(pos+1);
				string s1 = s.substr(0,pos);
				//cout<<s1<<" "<<s2<<endl;
				cout<<mp1[s1]*13+mp2[s2]<<endl;
			}		
			else 
			{
				if(mp1.find(s)!=mp1.end())
					cout<<mp1[s]*13<<endl;
				else cout<<mp2[s]<<endl;
			}
			
		}
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值