PAT Basic Level 1044 火星数字

题目链接:

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

AC代码:

 

#include 
#include 
#include 
#include 
#include 
#include 
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 strArray(str,str+12);//12为string数组的元素个数
/*3,初始化:
vector 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'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<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<

二刷:

#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

char diwei[12][5]={"jan", "feb","mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};
char gaowei[12][5]={"tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};

bool isDigit(string str){
    int len=str.size();
    for(int i=0;i='0'&&str[i]<='9'))
            return false;
    }
    return true;
}

int main(){
    map mpgw;
    map mpdw;
    mpdw["jan"]=1;
    mpdw["feb"]=2;
    mpdw["mar"]=3;
    mpdw["apr"]=4;
    mpdw["may"]=5;
    mpdw["jun"]=6;
    mpdw["jly"]=7;
    mpdw["aug"]=8;
    mpdw["sep"]=9;
    mpdw["oct"]=10;
    mpdw["nov"]=11;
    mpdw["dec"]=12;

    mpgw["tam"]=1;
    mpgw["hel"]=2;
    mpgw["maa"]=3;
    mpgw["huh"]=4;
    mpgw["tou"]=5;
    mpgw["kes"]=6;
    mpgw["hei"]=7;
    mpgw["elo"]=8;
    mpgw["syy"]=9;
    mpgw["lok"]=10;
    mpgw["mer"]=11;
    mpgw["jou"]=12;

    int n;
    cin>>n;
    getchar();//使用getline前必加getchar吸收回车
    for(int i=0;i5){
                string gw=str.substr(0,3);
                string dw=str.substr(4,3);
                int res=mpgw[gw]*13+mpdw[dw];
                printf("%d\n",res);
            }
            else if(len==4){//测试点1是对0-tret的判断
                printf("0\n");
            }
            else{
                string wei=str.substr(0,3);
                int res=0;
                if(mpgw[wei]!=0)
                    res=mpgw[wei]*13;
                else if(mpdw[wei]!=0)
                    res=mpdw[wei];
                printf("%d\n",res);
            }
        }
    }
}

参考网上大佬的代码: 

#include 
#include 
#include 
#include 
using namespace std;

int main(){
	string one[13] = { "tret" ,"jan", "feb", "mar","apr","may", "jun", "jly","aug", "sep", "oct", "nov","dec" },
           two[13] = { "tret","tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou" };
    int sum=0,n;
    string m;
    cin>>n;
    getchar();
    while(n--){
        getline(cin,m);
        if(isdigit(m[0])){//地球-->火星
            int res=atoi(m.c_str());
            if(res/13)
                cout<4){
                for(int i=0;i<13;i++){
                    if(m.substr(0,3)==two[i]){
                        sum+=i*13;
                        break;
                    }
                }
            }
            cout<

 

 

 

你可能感兴趣的:(PAT,PAT,Basic,Level)