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;
}

二刷:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cctype>
#include <map>
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<len;i++){
        if(!(str[i]>='0'&&str[i]<='9'))
            return false;
    }
    return true;
}

int main(){
    map<string,int> mpgw;
    map<string,int> 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;i<n;i++){
        string str;
        getline(cin,str);
        if(isDigit(str)){
            int num=atoi(str.c_str());
            int gw=num/13;
            int dw=num%13;
            if(gw&&dw)
                printf("%s %s\n",gaowei[gw-1],diwei[dw-1]);
            else if(!gw&&dw){
                printf("%s\n",diwei[dw-1]);
            }
            else if(gw&&!dw){//高位不为0,低位为0
                printf("%s\n",gaowei[gw-1]);
            }
            else if(!gw&&!dw){//测试点1是对0-tret的判断
                printf("tret\n");
            }
        }
        else{
            int len=str.size();
            if(len>5){
                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 <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
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<<two[res/13];
            if(res/13&&res%13)//这三个不是if-else if-else循环
                cout<<" "<<one[res%13];
            if (!(res/13))//只有个位
                cout<<one[res%13];
            cout<<endl;
        }
        else{
            int len=m.size();
            for(int i=0;i<13;i++){
                //只存在一组字母,
                if(m.substr(len-3,len)==two[i]){//这两个是if else if循环
                //只有十位
                    sum+=(i*13);
                    break;
                }
                else if(m.substr(len-3,len)==one[i]){
                //只有个位
                    sum+=i;
                    break;
                }
            }
            //存在两组字母
            if(len>4){
                for(int i=0;i<13;i++){
                    if(m.substr(0,3)==two[i]){
                        sum+=i*13;
                        break;
                    }
                }
            }
            cout<<sum<<endl;
            sum=0;
        }
    }
}

 

 

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值