PAT Basic Level 1044 火星数字

题目链接:

https://pintia.cn/problem-sets/994805260223102976/problems/994805279328157696

AC代码:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <vector>
#include <string.h>
using namespace std;

//string数组初始化问题
string low_pos[]={"tret","jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};
string high_pos[]={"tret","tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};
//2,初始化成vector数组:vector<string> strArray(str,str+12);//12为string数组的元素个数
/*3,初始化:
vector<string> strArray(10);
strArray[0] = "hello";
strArray[1] = "world";
strArray[2] = "this";
strArray[3] = "find";
strArray[4] = "gank";
strArray[5] = "pink";
strArray[6 ]= "that";
strArray[7] = "when";
strArray[8] = "how";
strArray[9] = "cpp";
*/

string low_pos_="jan, feb, mar, apr, may, jun, jly, aug, sep, oct, nov, dec";
string high_pos_="tam, hel, maa, huh, tou, kes, hei, elo, syy, lok, mer, jou";
//判断string型的str能否转换为数字
bool isDigital(string str){
    for(int i=0;i<str.size();i++){
        if(str.at(i)>'9'||str.at(i)<'0')
            return false;
    }
    return true;
}

int main(){
    int n;
    cin>>n;
    char tmp=getchar();//应该加在这儿
    while(n--){
        string s;
        getline(cin,s);//读取回车,不应该加在,
        if(isDigital(s)){//s是数字
            int consult=atoi(s.c_str())/13;//商(consult)
            int remainder=atoi(s.c_str())%13;//余数(remainder)
            if(consult==0)//无论高位,低位均不输出tret:0.
                cout<<low_pos[remainder]<<endl;
            else if(remainder==0)
                cout<<high_pos[consult]<<endl;
            else
                cout<<high_pos[consult]<<" "<<low_pos[remainder]<<endl;
        }
        else{

            string s_1,s_2="";
            s_1=s.substr(0,3);
            int high_flag=1;//判断高位是否存在
            if(high_pos_.find(s_1)==-1)//在高位不发现
            {
                high_flag=0;
                s_2=s.substr(0,3);
            }
            int flag=0;//判断是否存在低位
            //flag=1:高位,低位均存在
            //flag=0:只存在高位.
            if(s.size()>6){
                s_2=s.substr(4,6);
                flag=1;
            }
            if(low_pos_.find(s_1)!=-1)//只存在低位
            {
                flag=1;
            }
            int consult=0,remainder=0;
            for(int i=0;i<13;i++){
                if(!high_flag)
                    consult=0;
                else if(s_1==high_pos[i])
                    consult=i;
                if(flag)
                    if(s_2==low_pos[i])
                        remainder=i;
            }
            int result=consult*13+remainder;
            cout<<result<<endl;
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41755143/article/details/86545737