原题:https://pintia.cn/problem-sets/994805342720868352/problems/994805367156883456
这道题我学会了字符串按某个符号分割,输入整行到字符串等等,知识点详见
字符串分割:https://blog.csdn.net/qq_44761480/article/details/100542089
整行输入:https://blog.csdn.net/qq_44761480/article/details/98882802
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include<stack>
#include<queue>
#include<sstream>
using namespace std;
/*
A1100 Mars Numbers (20 分)
*/
string a[13] = { "tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec" };
string b[13] = { "####", "tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou" };
//将火星数转换成地球数, 输出
void MtoE(string str) {
istringstream in(str);
string sa, sb;
getline(in, sb, ' ');
getline(in, sa, ' ');
int a1 = 0, a2 = 0; //十位,个位
for (int i = 0; i < 13; i++) {
if (sb == b[i]) //前一位
a1 = i;
if (sa == a[i] || sb == a[i]) //加上该数小于13
a2 = i;
}
cout << a1 * 13 + a2;
}
//将地球数转换成火星数,输出
void EtoM(string str) {
int num = stoi(str);
if (num == 0)
{
cout << a[0];
return;
}
if (num / 13 != 0)
cout << b[num / 13] ;
if (num / 13 != 0 && num % 13 != 0)
cout << " ";
if (num % 13 != 0)
cout << a[num % 13];
}
int main() {
int N;
cin >> N;
getchar(); //清除缓冲区的'\n'
for (int i = 0; i < N; i++) {
string str;
getline(cin, str);
if (str[0] >= '0' && str[0] <= '9') {
EtoM(str);
}
else
MtoE(str);
cout << endl;
}
return 0;
}