PAT甲级--1100 Mars Numbers(20 分)【map映射+字符串处理】

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 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

解题 思路:这题一看就是映射,我们通过输入的是字符串,我们写2个函数,一个处理输入的字符串是数字的,另一个函数是处理字符串的,第一个函数,我们看数据范围就知道很小,所以我们直接通string中的数值转换,将字符串转化成数字,然后处理,我们分三种情况处理,第二个函数是输入的是字符串,我们就看它的长度,我们知道是不超过100的,所以最多也就2个小字符串,这个也分三种情况处理,长度分别为3,4或者大于4的,大于4的,我们就用substr取出来,前面那个小字符串一定是b数组的,后面那个小字符串就是a数组里面的,将他们加起来就好了。

#include<bits/stdc++.h>
using namespace std;
string a[]={"tret","jan","feb","mar","apr","may","jun","jly","aug","sep","oct","nov","dec"};
string b[]={"","tam","hel","maa","huh","tou","kes","hei","elo","syy","lok","mer","jou"};
void fun1(string s)
{
	int n=stoi(s);
	int x=n%13;
	int y=n/13;
	if(y==0) cout<<a[x]<<endl;//不管x是不是等于0都在a数组中
	else if(y!=0&&x==0) cout<<b[y]<<endl;
	else cout<<b[y]<<" "<<a[x]<<endl;
}
void fun2(string s)
{
    int len=s.length();
    if(len==4)
	{
		cout<<"0"<<endl;
		return ;
	 } 
    else if(len==3)
    {
    	for(int i=0;i<13;i++)
    	{
    		if(s==a[i]) 
    		{
    			cout<<i<<endl;
    			return ;
			}
			 if(s==b[i])
			{
				cout<<i*13<<endl;
				return ;
			}
		}
	}
	else
	{
		string s1=s.substr(0,3);
		string s2=s.substr(4,3);
		int sum=0;
		for(int i=0;i<13;i++)
    	{
    		if(s1==b[i]) sum+=i*13;
			if(s2==a[i]) sum+=i;
		}
		cout<<sum<<endl;
	}
		return;
}
int main(void)
{
	int n;
	scanf("%d",&n);getchar();
	while(n--)
	{
		string s;
		getline(cin,s);
		if(isdigit(s[0]))
		fun1(s);
		else
		fun2(s);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Imagirl1/article/details/82146891