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


IDEA

1.需要将相关字符存起来,采用map键值对存储,火星文为key,地球文为value

2.根据输入的字符串首字符判断输出的是地球文还是火星文

3.还需要判断输入火星文是一位的还是两位的

3.用到int sprintf( char *buffer, const char *format [, argument] … );把格式化的数据写到字符串中,如sprintf(s,"%d",num);

1100. Mars Numbers (20)_第1张图片

CODE

 #include<iostream>
 #include<cstring>
 #include<map>
 #include<cstdlib>
 using namespace std;
 int main(){
 	map<string,int> m;
 	m["tret"]=0;
	m["jan"]=1;m["feb"]=2;m["mar"]=3;m["apr"]=4;m["may"]=5;m["jun"]=6;
	m["jly"]=7;m["aug"]=8;m["sep"]=9;m["oct"]=10;m["nov"]=11;m["dec"]=12;
	m["tam"]=13;m["hel"]=26;m["maa"]=39;m["huh"]=52;m["tou"]=65;m["kes"]=78;
	m["hei"]=91;m["elo"]=104;m["syy"]=117;m["lok"]=130;m["mer"]=143;m["jou"]=156;
	char s1[12][4]={"jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};
	char s2[12][4]={"tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};	
 	int n;
	cin>>n;
	cin.get();
	//while(n--){
	for(int i=0;i<n;i++){
		string str,result;
		getline(cin,str);
		if(str=="tret"){
			cout<<0<<endl;
		}else if(str=="0"){
			cout<<"tret"<<endl;
		}else if(isdigit(str[0])){//输入的是地球文 数字0-169 
			int num=atoi(str.c_str());
			int k1,k2;
			k1=num%13;//个位 
			k2=num/13;//十位 
			if(k2){
				if(k1){
					result=s2[k2-1];
					result+=" ";
					result+=s1[k1-1];
				}else{
					result=s2[k2-1];
				}
			}else{
				result+=s1[k1-1];
			}
			cout<<result<<endl;
		}else if(str.size()==3){//输入的是一个火星文 
			char s[4];
			sprintf(s,"%d",m[str]);
			result=s;
			cout<<result<<endl; 
		}else if(str.size()==7){//输入的是两个火星文字 
			string sub;
			int num;
			sub=str.substr(0,3);//十位0 1 2 ,3位是空格 
			num=m[sub];
			sub=str.substr(4,3);//个位4 5 6 
			num+=m[sub];
			char s[4];
			sprintf(s,"%d",num);
			result=s;
			cout<<result<<endl;
		} 
	} 
 	return 0;
 }


你可能感兴趣的:(进制转换,pat)