PAT-PAT (Advanced Level) Practise 1100 Mars Numbers (20)【二星级】

题目链接:http://www.patest.cn/contests/pat-a-practise/1100

题面:

1100. Mars Numbers (20)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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的倍数后面一位的0是被省略的,代码写的比较搓,A了就好。


代码:

#include <iostream>
#include <string>
#include <cstdio>
#include <map>
using namespace std;
map <int,string> r1;
map <int,string> r2;
//地球到火星
void trans1(string x)
{
	int res=0,t=1;
	for(int i=x.length()-1;i>=0;i--)
		res+=(x[i]-'0')*t,t*=10;
	//13的倍数特判
	if(res%13==0)
	{
		cout<<r2[res/13]<<endl;
		return;
	}
	if(res>12)
        cout<<r2[res/13]<<" "<<r1[res%13]<<endl;
	else
		cout<<r1[res]<<endl;
}
//火星到地球
void trans2(string x)
{
	//0特判
	if(x=="tret")
	{
		cout<<"0\n";
		return;
	}
	string s1,s2;
	int v1,v2;
	s1=x.substr(0,3);
	//s1为高位
	if(x.length()>3)
	{
		s2=x.substr(4);
		map <int,string> :: iterator it;
		for(it=r1.begin();it!=r1.end();it++)
		{
			if(s2==(it->second))
			{
				v2=it->first;
				break;
			}
		}
		for(it=r2.begin();it!=r2.end();it++)
		{
			if(s1==(it->second))
			{
				v1=it->first;
				break;
			}
		}
		cout<<v2+v1*13<<endl;
	}
	//s1为可能为低位,也可能为高位,因为13的倍数特殊
	//用flag标记
	else
	{
	  bool flag=false;
      map <int,string>::iterator it;
	  for(it=r1.begin();it!=r1.end();it++)
		{
			if(s1==(it->second))
			{
				flag=true;
				v1=it->first;
				break;
			}
		}
	  if(flag)
	  {
	    cout<<v1<<endl;
	  }
	  else
	  {
        for(it=r2.begin();it!=r2.end();it++)
		{
			if(s1==(it->second))
			{
				flag=true;
				v1=it->first;
				break;
			}
		}
        cout<<v1*13<<endl;
	  }
	}
}
int main()
{
	r1[0]="tret",r2[0]="tret";
	r1[1]="jan",r2[1]="tam";
	r1[2]="feb",r2[2]="hel";
	r1[3]="mar",r2[3]="maa";
	r1[4]="apr",r2[4]="huh";
	r1[5]="may",r2[5]="tou";
	r1[6]="jun",r2[6]="kes";
	r1[7]="jly",r2[7]="hei";
	r1[8]="aug",r2[8]="elo";
	r1[9]="sep",r2[9]="syy";
	r1[10]="oct",r2[10]="lok";
	r1[11]="nov",r2[11]="mer";
	r1[12]="dec",r2[12]="jou";
    string s;
	int n;
	cin>>n;
	getchar();
	while(n--)
	{
		getline(cin,s);
		if(s[0]>='0'&&s[0]<='9')
		{
			trans1(s);
		}
		else
			trans2(s);
	}
	return 0;
}


你可能感兴趣的:(模拟,pat)