PAT 1100 Mars Numbers

13 篇文章 0 订阅

 字符串处理

#include <iostream>
#include <vector>

using namespace std;
string strs1[12]={"tam","hel","maa","huh","tou","kes",
                                            "hei","elo",
                                            "syy","lok","mer","jou"};
string strs2[13]={"tret","jan","feb","mar","apr","may","jun","jly",
                 "aug","sep","oct","nov","dec"};
string findNum(string& s){
    if(isdigit(s[0])){
        int num=stoi(s);
        int a=num/13;
        int b=num%13;
        if(a==0&&b==0){
            return strs2[b];
        } else if(a==0&&b!=0){
            return strs2[b];
        }else if(a!=0&&b==0){
            return strs1[a-1];
        } else{
            return strs1[a-1]+" "+strs2[b];
        }


    } else{
        if (s.find(' ')==string::npos){
            for (int i = 0; i < 13; ++i) {
                if(strs2[i]==s){
                    return to_string(i);
                }
            }
            for (int j = 0; j < 12; ++j) {
                if(strs1[j]==s){
                    return to_string(13*(j+1));
                }
            }
        } else{
            int index=s.find(' ');
            string s1=s.substr(0,index);
            string s2=s.substr(index+1,s.length()-index);
            int a=0,b=0;
            for (int i = 0; i < 12; ++i) {
                if(strs1[i]==s1){
                    a=i+1;
                    break;
                }
            }
            for (int j = 0; j < 13; ++j) {
                if(s2==strs2[j]){
                    b=j;
                    break;
                }
            }
            return to_string(a*13+b);
        }
    }
}
int main() {
    int N;
    cin>>N;
    cin.get();
    vector<string> vector1;
    for (int i = 0; i < N; ++i) {
        string s;
        getline(cin,s);
        vector1.push_back(findNum(s));
    }
    for(auto x:vector1){
        cout<<x<<endl;
    }
    return 0;
}

二刷:

#include <iostream>
#include<vector>
using namespace std;

string array1[13]={"tret","jan","feb","mar","apr","may","jun",
"jly","aug","sep","oct","nov","dec"
};
string array2[12]={"tam","hel","maa","huh","tou","kes","hei","elo",
"syy","lok","mer","jou"
};
string fun1(string s1){
    int val=stoi(s1);
    int a=val/13;
    int b=val%13;
    string res="";
    if(a!=0){
        res+=array2[a-1];
         if(b==0){
         return res;
        }
        res+=" ";
    }
    res+=array1[b];
    return res;
}
string fun2(string s1){
      int res=0;
    if(s1.find(' ')==string::npos){
        for(int i=0;i<13;i++){
            if(array1[i]==s1){
                return to_string(i);
            }
        }
        for(int i=0;i<12;i++){
            if(array2[i]==s1){
                return to_string((i+1)*13);
            }
        }
    }else{
        int index=s1.find(' ');
        string s2=s1.substr(0,index);
        string s3=s1.substr(index+1);
         for(int i=0;i<13;i++){
            if(array1[i]==s3){
                res+=i;
            }
        }
        for(int i=0;i<12;i++){
            if(array2[i]==s2){
                res+=((i+1)*13);
            }
        }
    }
    return to_string(res);
}
int main()
{
    int N;
    cin>>N;
    getchar();
    vector<string> vector1;
    for(int i=0;i<N;i++){
        string s1;
      //  cin>>s1;
      getline(cin,s1);
        if(isdigit(s1[0])){
            vector1.push_back(fun1(s1));
        }else{
            vector1.push_back(fun2(s1));
        }
    }
    for(int i=0;i<vector1.size();i++){
        cout<<vector1[i]<<endl;
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值