PAT甲级 1100 Mars Numbers 字符串模拟

41 篇文章 1 订阅
15 篇文章 0 订阅

在这里插入图片描述
在这里插入图片描述

代码如下:

//字符串模拟
#include<iostream>
#include<string>
#include<map>
#include<stdio.h>
#include<string.h>
using namespace std;

string first[12]={"tam","hel","maa","huh","tou","kes","hei","elo","syy","lok","mer","jou"};
string second[12]={ "jan","feb","mar","apr","may","jun","jly","aug","sep","oct","nov","dec"};
map<string,int> mp1;
map<string,int> mp2;

string s;
int n;

int to_num(string s){
    int ans=0;
    for(int i=0;i<s.length();i++){
        ans=ans*10+s[i]-'0';
    }
    return ans;
}

int main(){
    for(int i=0;i<12;i++){
        mp1[first[i]]=i+1;
        mp2[second[i]]=i+1;
    }

    cin>>n;
    getchar();
    for(int i=0;i<n;i++){
        getline(cin,s);
        if(s[0]>='0'&&s[0]<='9'){
            int num=to_num(s);
            int fir=num/13;
            int sec=num-fir*13;
            if(fir!=0&&sec!=0){
                cout<<first[fir-1]<<" "<<second[sec-1];
            }else if(fir==0&&sec!=0){
                cout<<second[sec-1];
            }else if(sec==0&&fir!=0){
                cout<<first[fir-1];
            }else{
                cout<<"tret";
            }
            if(i!=n-1){
                cout<<endl;
            }
        }else{
            if(s.length()>3){
                string s1="";
                for(int j=0;j<3;j++){
                    s1=s1+s[j];
                }
                string s2="";
                if(s[3]!=' '){
                    for(int j=5;j<8;j++){
                        s2=s2+s[j];
                    }
                }else{
                    for(int j=4;j<7;j++){
                        s2=s2+s[j];
                    }
                }
                int fir=0,sec=0;
                if(s1!="tret"){
                    fir=mp1[s1];
                }
                if(s2!="tret"){
                    sec=mp2[s2];
                }
                cout<<fir*13+sec;
                if(i!=n-1){
                    cout<<endl;
                }
            }else{
                string s1="";
                for(int j=0;j<3;j++){
                    s1=s1+s[j];
                }
                if(mp1[s1]!=0){
                    cout<<mp1[s1]*13;
                }
                if(mp2[s1]!=0){
                    cout<<mp2[s1];
                }
                if(i!=n-1){
                    cout<<endl;
                }
            }
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值