PAT1100 Mars Numbers

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

注意点:在c++中输入带空格的字符串可用cin.getline(str1,len)(str1是字符数组char[])或getline(cin.str2)(str2是字符串类型string)。用getline()函数进行循环输入时,由于cin函数是直接从缓冲区取数据的,所以当缓冲区有残留数据时,cin函数会直接取得这些残留数据而不会请求键盘输入。  因此,当用getline()函数输入之前有数据输入的话,要将之前的缓冲区内的残留数据清除。

      此题中用getchar()吃掉之前的回车输入。

AC代码:

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

map<int,string> T1,T2; 
map<string,int> E1,E2;
int main()
{
	T1[0] = "tret";T2[0] = "";
	T1[1] = "jan"; T1[2] = "feb"; T1[3] = "mar";
	T1[4] = "apr"; T1[5] = "may"; T1[6] = "jun";
	T1[7] = "jly"; T1[8] = "aug"; T1[9] = "sep";
	T1[10] = "oct"; T1[11] = "nov"; T1[12] = "dec";
	T2[1] = "tam"; T2[2] = "hel"; T2[3] = "maa";
	T2[4] = "huh"; T2[5] = "tou"; T2[6] = "kes";
	T2[7] = "hei"; T2[8] = "elo"; T2[9] = "syy";
	T2[10] = "lok"; T2[11] = "mer"; T2[12] = "jou";
	E1["jan"] = 1; E1["feb"] = 2; E1["mar"] = 3;
	E1["apr"] = 4; E1["may"] = 5; E1["jun"] = 6;
	E1["jly"] = 7; E1["aug"] = 8; E1["sep"] = 9;
	E1["oct"] = 10; E1["nov"] = 11; E1["dec"] = 12;
	E2["tam"] = 1; E2["hel"] = 2; E2["maa"] = 3;
	E2["huh"] = 4; E2["tou"] = 5; E2["kes"] = 6;
	E2["hei"] = 7; E2["elo"] = 8; E2["syy"] = 9;
	E2["lok"] = 10; E2["mer"] = 11; E2["jou"] = 12;
	
	freopen("9.txt","r",stdin);
	int N;
	scanf("%d",&N);
	getchar();
	for(int i = 0; i< N; i++){
		string p;
		getline(cin,p);
		if(p[0]>='0'&&p[0]<='9'){
			int m = 0, n = 1;
			for(int j = p.size() - 1; j>=0; j--){
				m +=  (p[j] - '0')*n;
				n = n*10;
			}
			printf("%s",T2[m/13].c_str());
			if(m/13 && m%13) printf(" %s",T1[m%13].c_str());
			else if(!(m/13) && m%13)   printf("%s",T1[m%13].c_str());
			else if(!(m/13) && m%13 == 0) printf("%s",T1[m%13].c_str());
			printf("\n");
		}else{
			int m, n;
			if(p.size() > 3){
				m = E2[p.substr(0,3)];
				n = E1[p.substr(4,3)];
				printf("%d\n",m*13 + n);
			}else{
				if(E1.find(p) != E1.end()){
					printf("%d\n",E1[p]);
				}else{
					printf("%d\n",E2[p]*13);
				}
			}
		
		}
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_41168353/article/details/81200360