PAT甲级1100.

目录

A1100

题目

样例

输入:

4
29
5
elo nov
tam

输出:

hel mar
may
115
13

思路和坑点

  进制转换,按照字符串读入,判断第一个字符是数字还是字母,进行相应的转换,这里使用map和string类数组进行双向的映射,也可以使用遍历数组的方式完成字符串到数字的映射关系,对数字进行处理时可以使用sscanf直接将string读成数字。(所给的字符串一定要拼写正确)
  坑点
  根据样例,如果是整13的倍数,只有高位,没有低位。

AC代码

#include<bits/stdc++.h>
using namespace std;
//字符数组 
string low[]={"tret","jan","feb","mar","apr","may","jun","jly","aug","sep","oct","nov","dec"};
string high[]={"","tam","hel","maa","huh","tou","kes","hei","elo","syy","lok","mer","jou"};
map<string,int> lowmp,highmp;                       //字符到数字的映射 
void string_int(string str){                        //字符转成数字 
    int ans=0;
    if(str.size()<5){
        if(highmp.find(str)!=highmp.end()){         //如果只是单一的高位,即13的倍数 
            ans=13*highmp[str];
        }
        else
            ans=lowmp[str];                         //如果时只有低位 
    }                                               //如果高位和低位同时存在 
    else ans=highmp[str.substr(0,3)]*13+lowmp[str.substr(4,3)];
    cout<<ans<<'\n';    
}
void int_string(string str){                        //数字转成字符 
    int num;
    sscanf(str.c_str(),"%d",&num);
    if(num<13)                                      //小于13的只有低位 
        cout<<low[num];
    else{
        cout<<high[num/13];                         
        if(num%13) cout<<' '<<low[num%13];          //如果不是13的倍数,还需要输出低位 
    }    
    putchar('\n');
}
int main(void){
#ifdef ONLINE_JUDGE
#else
    freopen("1.txt","r",stdin);
#endif
    int n;
    string temp;
    for(int i=0;i<13;i++){                          //初始化字符串到数字的映射 
        lowmp[low[i]]=i;
        highmp[high[i]]=i;
    } 
    cin>>n;
    getchar();                                      //吸收第一行末尾的换行 
    for(int i=0;i<n;i++){
        getline(cin,temp);                          //整行读入字符串 
        if(isalpha(temp[0]))
            string_int(temp);
        else
            int_string(temp);
    } 
    return 0;
}