PAT1100. Mars Numbers (20)

解析PAT 2015火星数字系统转换题,介绍如何实现地球与火星数字系统的互译,包括进制转换、字符串处理等关键技术。

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

本题是PAT2015.9.12-level A的第一题。我参加了该考试,但在此题答题过程中并没有通过所有的测试用例。回到家后重新整理了下。

题目信息如下:

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.

详见http://www.patest.cn/contests/pat-a-practise/1100


简单分析而言,此题是几个小问题的糅合:进制转换,字符串与数字转换。字符串处理。

数据结构方面,因为题目中给定了输入数据范围在100以内,故偷懒直接使用了100的定长数组。3个维度分别用于表示两个13进制位和输出语言标志位。

算法方面,使用stl的查找算法。

本题的特殊测试点在于,mars上的0是一个特殊的存在,整13倍数的数(13,26,39...)在表达时是不能出现0的。双向翻译时都需要注意这一点。

代码如下:

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

int output[100][3];
string ten[13]={"",	"tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};
string one[13]={"tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};

#define debug

int getValue(string input)
{
	vector<string> tens(ten, ten+13), ones(one, one+13);		
	vector<string>::iterator iter = std::find(tens.begin(), tens.end(), input);
	if(iter == tens.end()){
		iter = std::find(ones.begin(), ones.end(), input);
		return (iter - ones.begin());
	}
	else{
		return 13*(iter - tens.begin());
	}
}


int main(void)
{
#ifdef debug
	freopen("input.txt", "r", stdin);
#endif

	int count;
	string temp;

	cin>>count;
	getchar();

	for(int i=0;i<count;i++)
	{
		std::getline(cin, temp);
		if(temp[0]>'9' || temp[0]<'0'){
			//char
			string::size_type pos = temp.find(' ');
			if(string::npos == pos){
				//only 1 char
				int temp_value =  getValue(temp);
				if(temp_value > 12){
					output[i][0] = temp_value / 13;
					output[i][1] = 0;
				}
				else{
					output[i][0] = 0;
					output[i][1] = temp_value;
				}
			}
			else{
				//2 char
				string t1(temp.begin(), temp.begin()+pos), t2(temp.begin()+pos+1, temp.end());
				output[i][0] = getValue(t1)/13;
				output[i][1] = getValue(t2);
			}
			output[i][2] = 0;	//output in ineger
		}
		else{
			int temp_value = std::atoi(temp.c_str());
			//integer
			output[i][0] = temp_value / 13;
			output[i][1] = temp_value % 13;
			output[i][2] = 1;	//output in chars.
		}
	}


	//output
	for(int i=0;i<count;i++){
		if(output[i][2] == 0){
			//output as integer
			cout<<output[i][0]*13 + output[i][1]<<endl;
		}
		else{
			//output as chars.
			if(output[i][0] != 0 && output[i][1]==0){		/////////当整13的倍数时,0不予显示。
				cout<<ten[output[i][0]]<<endl;
			}
			else if(output[i][0] != 0 && output[i][1]!=0){
				cout<<ten[output[i][0]]<<' '<<one[output[i][1]]<<endl;
			}
			else{
				cout<<one[output[i][1]]<<endl;
			}
		}
	}

	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值