STL--PAT.A1100 & B.1044 [string & map]

来源:互联网 发布:maka h5制作软件下载 编辑:程序博客网 时间:2024/06/05 15:32

【2017/3/5】

1、使用getline(cin, str) 读入一行时,注意不要读入换行符哪!
2、map【string, int】 中string可以带空格

/**********************3stone*****************************Problem: PAT.A1100.火星数字(20)Author:3stoneTime:2017/3/5***********************3stone****************************/#include<cstdio>#include<iostream>#include<cstring>#include<string>#include<map>using namespace std;string low[14] = {"tret","jan","feb","mar","apr","may","jun","jly","aug","sep","oct","nov","dec"};string high[14] = {"tret","tam","hel","maa","huh","tou","kes","hei","elo","syy","lok","mer","jou"};string etom[300];map<string, int> mtoe;int main(){    for(int i = 0; i < 13; i++){//初始化         mtoe[low[i]] = i;        mtoe[high[i]] = i * 13;        etom[i] = low[i];        etom[i * 13] = high[i];    }    for(int i = 1; i < 13; i++)//组成火星文的两位数【赞】         for(int j = 1; j < 13; j++){//个位             string str = high[i] + " " + low[j];            etom[i * 13 + j] = str;            mtoe[str] = i * 13 + j;        }    int n;    scanf("%d%*c", &n);    string str;     for(int i = 0; i < n; i++){        int num = 0;        getline(cin, str);        if(str[0]>='0' && str[0] <= '9'){//数字             for(int j = 0; j < str.length();j++)                num = num * 10 + (str[j] - '0');            printf("%s\n", etom[num].c_str());      //      cout << etom[num] << endl;           }                   else{//火星             //怎样判断是一位数还是两位数呢             cout << mtoe[str] << endl;         }       }//for-i     return 0;}
0 0
原创粉丝点击