PAT A1100:Mars Numbers

题目描述

1100 Mars Numbers (20分)
People on Mars count their numbers with base 13:

Zero on Earth is called “tret” on Mars.The numbers 1 to 12 on Earth 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的整数,直接对应low数组
    • 大于13且能被13整除的整数,直接对应high数组
    • 大于13且不能被13整除的整数,商对应high数组,余数对应low数组
  • 当输入的字符串:
    • 无空格字符,则表示要么是小于13的整数,要么是大于13且能被13整除的整数;
    • 有空格字符,先转换空格前的字符,再转换后面的字符

代码实现

#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
using namespace std;
char low[13][5]={"tret","jan","feb","mar","apr","may","jun","jly","aug","sep","oct","nov","dec"};
char high[13][5]={"","tam","hel","maa","huh","tou","kes","hei","elo","syy","lok","mer","jou"};
int N;
int changetonum(char str[]){
	for(int i=0;i<13;i++)
		if(strcmp(low[i],str)==0) return i;
	for(int i=1;i<13;i++)
		if(strcmp(high[i],str)==0) return i*13;
}
void solve()
{
    scanf("%d",&N);
	getchar();
	for(int i=0;i<N;i++){
        string s;
		getline(cin,s);
		if(s[0]>='0'&&s[0]<='9'){  //地球数字 
			int sum=0;
			for(int j=0;j<s.size();j++)
				sum=10*sum+(s[j]-'0');
			if(sum<13) printf("%s\n",low[sum]);
			else if(sum%13==0) printf("%s\n",high[sum/13]);
			else if(sum%13!=0) printf("%s %s\n",high[sum/13],low[sum%13]);
		}else{  //火星数字 
			char str[6];
			if(s.size()<7){
				strcpy(str,s.c_str());
				printf("%d\n",changetonum(str));
			}else{
				string temp=s.substr(0,3);
				strcpy(str,temp.c_str());
				int sum=changetonum(str);
				temp=s.substr(4,3);
				strcpy(str,temp.c_str());
				printf("%d\n",sum+changetonum(str));
			}
		}
	}
}
int main(){
	solve();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值