您的位置:首页 > 其它

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

2017-03-05 11:45 344 查看
思路:

1、使用getline(cin, str) 读入一行时,注意不要读入换行符哪!

2、map【string, int】 中string可以带空格

/**********************3stone*****************************
Problem: PAT.A1100.火星数字(20)
Author:3stone
Time: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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: