PAT-1100(Mars Numbers)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PAT-1100(Mars Numbers)相关的知识,希望对你有一定的参考价值。

  题目见这里

   题目并不难,不过一开始我没能理清题意,参考了下这里,明白题目实际是考进制转换(13进制),外加一个映射(hash),这当然比较简单啦!需要注意的是,样例中tam(Mars Number)----->13(Earth),由此可知,以13的整倍数(Earth)出现时,不带‘tret’(earth:0)

#include <cstdio>
#include <iostream>
#include <map>
#include <cstring>

using namespace std;

struct ptrCmp{
	bool operator()(const char *s1, const char *s2) const{
		return strcmp(s1,s2)<0;
	}
};

int main(){
	const char hash[25][5]={"tret","jan","feb","mar","apr","may","jun","jly","aug","sep","oct","nov","dec",
	 "tam","hel","maa","huh","tou","kes","hei","elo","syy","lok","mer","jou"};
	map<const char*,int,ptrCmp>hash1;
	int i;
	for(i=0;i<25;i++)
		hash1.insert(pair<const char*,int>(hash[i],i));  
//  freopen("Data.txt","r",stdin);
	char s[5],s1[5],c;
	int n;
	scanf("%d",&n);
	getchar();
	while(n--){
		scanf("%s",s);
		scanf("%c",&c);
		if(c==‘ ‘ || (s[0]>=‘a‘ && s[0]<=‘z‘)){
			if(c==‘ ‘) scanf("%s",s1);
			if(c==‘\n‘){
				int index = hash1[s];
				if(index<13) printf("%d",index);
				else printf("%d",(index-12)*13);
			}
			else printf("%d",(hash1[s]-12)*13+hash1[s1]);
			printf("\n");
		}
		else{
			int key,k;
			key=0,k=0;
			while(s[k]) key = 10*key+s[k]-‘0‘, k ++;
			if(key<13) printf("%s\n",hash[key]);
			else{
				printf("%s",hash[key/13+12]);	
				if(key%13) printf(" %s",hash[key%13]); //不带‘tret‘ 
				printf("\n");
			}
		}
	}
	return 0;
}

 

以上是关于PAT-1100(Mars Numbers)的主要内容,如果未能解决你的问题,请参考以下文章

pat 1100 Mars Numbers(20 分)

PAT-1100(Mars Numbers)

B1044/A1100:Mars Numbers (20)

1100. Mars Numbers (20)

1100. Mars Numbers (20)字符串处理——PAT (Advanced Level) Practise

1100. Mars Numbers (20)字符串处理——PAT (Advanced Level) Practise