1044 火星数字

AC代码

写的很乱,用了多层if-else。随手记录一下,懒得优化。

  • 低位为 0 时,在火星字中不输出,如,tam等于13而不是tam tret
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
using namespace std;

string mars_low[13]={
    "tret","jan","feb","mar","apr","may","jun","jly","aug","sep","oct","nov","dec",     
};
string mars_up[12]={
    "tam","hel","maa","huh","tou","kes","hei","elo","syy","lok","mer","jou"
};

/*查找高位火星字*/
int find_up(string ch){	
    for(int i=0;i<12;i++){
        if(mars_up[i]==ch){	//"abc"=="abcde",与C++string类型重载了==运算符有关
            return i;
        }
    }
	return -1;
}

/*查找低位火星字*/
int find_low(string ch){
    for(int i=0;i<13;i++){
        if(mars_low[i]==ch){
            return i;
        }
    }
    return -1;
}

int main(){
    int n;
    scanf("%d",&n);
    getchar();	//后面的getline()会吃回车,用getchar()吃掉
    string ch;	
    for(int i=0;i<n;i++){
        getline(cin,ch);	
        if('0'<=ch[0]&&ch[0]<='9'){		//第一个字符是数字就可以判定为是数字
        	int num=stoi(ch);	//转成int类型
        	if(num<=12){	//小于等于12直接输出该火星字
        		cout<<mars_low[num]<<"\n";
			}
			else{	//大于12时需要判断是否为整13数(13的倍数),低位0是不输出的
				if(num%13==0){	//不输出低位0
					cout<<mars_up[num/13-1]<<"\n";
				}
				else{	//分别输出高位与低位
					cout<<mars_up[num/13-1]<<" "<<mars_low[num%13]<<"\n";
				}
			} 	
        }
        
        else{	//不为数字时
        	int lowx=find_low(ch);//试图在低位表中查找该字符对应的索引
            if(lowx>=0){	//lowx大于等于0时,表示在低位表中查到,直接输出
                cout<<find_low(ch)<<"\n";
            }
            else{	//开头不为低位必为高位
            	if(ch.size()>3){	//高低位同时有
					string st=&ch[4];
            		ch[3]='\0';
					char *upx=&ch[0];
            		lowx=(find_up(upx)+1)*13+find_low(st);
            		cout<<lowx<<"\n";
				}else{	//仅有高位,即为整13数
					cout<<(find_up(ch)+1)*13<<"\n";
				}            	
			}
        }
    }
    return 0;
}

版权声明:本文为weixin_44421292原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。