C++ STL map B1044/A1100.火星数字(读取带空格的string : 使用getline(cin,str)函数)

用了打表的技巧

#include <bits/stdc++.h>
#include<math.h>
#include <string>
using namespace std;
const int maxn = 40010;//最大学生人数
//[0,12]的火星文
string unitDigit[13]  = {"tret","jan","feb","mar","apr","may","jun","jly","aug","sep","oct","nov","dec"};
//13的[0,12]倍的火星文
string tenDigit[13] = {"tret","tam","hel","maa","huh","tou","kes","hei","elo","syy","lok","mer","jou"};
string numToStr[170];//数字——>火星文
map<string,int> strToNum;//火星文——>数字
void init(){
    for(int i =0;i<13;++i){
        numToStr[i] = unitDigit[i];//个位为[0,12],十位为0
        strToNum[unitDigit[i]] = i;
        numToStr[i * 13] = tenDigit[i];//十位为[0,12],个位为0
        strToNum[tenDigit[i]] = i * 13;
    }
    for(int i = 1;i<13;++i){
        for(int j = 1;j<13;++j){
            string str = tenDigit[i] + " " + unitDigit[j];
            numToStr[i * 13 + j] = str;//数字——>火星文
            strToNum[str] = i * 13 + j; //火星文——>数字
        }
    }
}
int main(){
    init();//打表
    int T;
    scanf("%d%*c",&T);
    while(T--){
        string str;
        //cin>>str;
        getline(cin,str);
        if(str[0] >= '0' && str[0] <= '9'){
            int num = 0;//字符串转换为数字
            for(int i=0;i<str.length();++i){
                num = num * 10 + (str[i] - '0');
            }
            cout<<numToStr[num]<<endl;
        }else{
            cout<<strToNum[str]<<endl;
        }
    }
    system("pause");
    return 0;
} 

猜你喜欢

转载自www.cnblogs.com/JasonPeng1/p/12203365.html