PAT - 甲级 - 1100. Mars Numbers (20)(字符串处理)

本文介绍了一个程序设计问题,该程序能实现地球十进制数与火星十三进制数之间的互转。火星计数系统使用特定的词汇表示数值,文章详细解析了输入输出规格,并提供了一段C++代码示例来解决这一问题。

摘要生成于 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

题目思路:

进行十三进制和十进制的互相转换,十进制的常规意义上的,十三进制是题目重新规定的,给出了高位和低位的表示方法。

1.13的倍数可以直接用1个高位“火星文”表示,不需要在低位补“tret”即"0"。

2.当输入的“火星文”只有一个单词的时候,需要判断是高位还是低位。

题目代码:

#include <cstdio>
#include <string>
#include <map>
#include <iostream>
using namespace std;

map<string, int>m1; // 低位 
map<string, int>m2; // 高位 
map<string, int>m3; // 低位+高位 

map<int, string>n1; // 低位
map<int, string>n2; // 高位 
map<int, string>n3; // 低位+高位 
int num1[13] = {0,1,2,3,4,5,6,7,8,9,10,11,12};
int num2[26] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25};
string str1[13] = {"tret","jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};
string str2[13] = {"tret","tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};
string str3[26] = {"tret","jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec",
				 "tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};
string s1;
int n;
int main(){
	scanf("%d",&n);
	// 初始化 高 低位 
	for(int i = 0; i < 13; i++){
		m1[str1[i]] = num1[i]; 
		m2[str2[i]] = num1[i]; 	 
		n1[num1[i]] = str1[i]; 
		n2[num1[i]] = str2[i];
	}
	// 初始化 全 
	for(int i = 0; i < 26; i++){
		m3[str3[i]] = num2[i]; 
		n3[num2[i]] = str3[i];  
	}
	getchar();
	for(int i = 0; i < n; i++){
		getline(cin,s1);		
		//数字 
		if(s1[0] >= '0' && s1[0] <= '9'){
			int temp;
			sscanf(s1.c_str(),"%d",&temp);
			if(temp < 13)
				cout<<n1[temp]<<endl;
			else if(temp % 13 == 0)
				cout<<n2[temp/13]<<endl; 
			else
				cout<<n2[temp/13]<<" "<<n1[temp%13]<<endl;
		}
		// 字符串 
		else{
			if(s1.length() == 3)
				if(m3[s1] >= 13)
					cout<<(m3[s1]-12) * 13<<endl; 
				else
					cout<<m3[s1]<<endl;
			else if(s1.length() == 4) 
				cout<<0<<endl;
			else{
				cout<<(m2[s1.substr(0,3)])*13+m1[s1.substr(4,3)]<<endl;
			}			 
		}
	}	
	return 0;
} 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值