zoukankan      html  css  js  c++  java
  • 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=='
    '){
    				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("
    ");
    		}
    		else{
    			int key,k;
    			key=0,k=0;
    			while(s[k]) key = 10*key+s[k]-'0', k ++;
    			if(key<13) printf("%s
    ",hash[key]);
    			else{
    				printf("%s",hash[key/13+12]);	
    				if(key%13) printf(" %s",hash[key%13]); //不带'tret' 
    				printf("
    ");
    			}
    		}
    	}
    	return 0;
    }
    
  • 相关阅读:
    面向接口程序设计思想实践
    Block Chain Learning Notes
    ECMAScript 6.0
    Etcd Learning Notes
    Travis CI Build Continuous Integration
    Markdown Learning Notes
    SPRING MICROSERVICES IN ACTION
    Java Interview Questions Summary
    Node.js Learning Notes
    Apache Thrift Learning Notes
  • 原文地址:https://www.cnblogs.com/emptyCoder/p/7140992.html
Copyright © 2011-2022 走看看