1044 火星数字 (20 分)【使用atoi( )函数】

代码:

#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;

int main(){
    int n;
    char low[13][5]={"tret","jan", "feb", "mar", "apr", "may",
                     "jun","jly", "aug", "sep", "oct", "nov", "dec"};
    char high[13][5]={"tret","tam", "hel", "maa", "huh", "tou",
                      "kes", "hei", "elo", "syy", "lok", "mer", "jou"};
    cin>>n;
    cin.get();
    while(n--){
        int h=0,l=0,sum=0;
        char a[20]={0};
        cin.getline(a,10005);
        if(a[0]>='0'&&a[0]<='9'){
            sum=atoi(a);
            l=sum%13;
            h=sum/13;
            if(sum<13)
                printf("%s\n",low[sum]);
            else if(sum>=13&&l==0){
                printf("%s\n",high[h]);
            }
            else{
                printf("%s %s\n",high[h],low[l]);
            }
        }
        else{
            int i,k=0,tag=0;
            for(i=0;i<strlen(a);i++){
                if(a[i]==' '){
                    tag=1;
                    break;
                }
            }
            if(tag){
                k=i+1;
                for(int j=0;j<13;j++){
                    if(equal(a,a+i,high[j])){
                        h=j;
                        break;
                    }
                }
            }
            int find=0;
            for(int j=0;j<13;j++){
                if(strcmp(a+k,low[j])==0){
                    l=j;
                    find=1;
                    break;
                }
            }
            if(find==0){
                for(int j=0;j<13;j++){
                    if(strcmp(a+k,high[j])==0){
                        h=j;
                        break;
                    }
                }
            }
            printf("%d\n",h*13+l);
        }
    }
    return 0;
}
//   1    2    3    4    5    6    7    8    9   10   11   12
//  tam, hel, maa, huh, tou, kes, hei, elo, syy, lok, mer, jou
//  jan, feb, mar, apr, may, jun, jly, aug, sep, oct, nov, dec

知识点:

  1. atoi、stoi、strtoi区别
  2. c++中c_str()的用法详解
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值