判断输入的是火星文数字还是地球数字,如果是地球数字就变成火星文数字,如果是火星文数字就变成地球数字,然后输出。
函数使用:
引入头文件#include<string>
stoi()中放入(string)类型的参数,可以把string转换成int类型
所以数字的类型不能超过int,不然会报错,而且在转换的过程中好像会发生强转。
例如
int n=stoi("1.234");
cout<<n;
此时会输出1。
慎用~
在某些题目需要处理字符串时,这些题目往往是输入的一行中包含多个字符以及空格,这个时候就可以利用 stringstream进行单个字符或者单个字符串分析处理了
1.头文件:#include
2.stringstream是C++提供的串流(stream)物件。
3.stringstream的作用就是从string对象读取字符或字符串。
#include<iostream>
#include<map>
#include<string>
#include<vector>
#include<cctype>
#include<sstream>
using namespace std;
string a[13] = { "tret","jan","feb","mar","apr","may","jun","jly","aug","sep","oct","nov","dec" };
string b[20] = { "","tam","hel","maa","huh","tou","kes","hei","elo","syy","lok","mer","jou" };
int main()
{
int n;
cin >> n;
string s;
getline(cin, s);
for (int i = 0; i < n; i++)
{
getline(cin, s);
if (s[0] >= '0' && s[0] <= '9')
{
int temp = stoi(s);
if (temp <= 12)cout << a[temp]<<endl;
else if (temp && temp % 13 == 0)cout << b[temp / 13] << endl;
else cout << b[temp / 13] << " " << a[temp % 13] << endl;
}
else
{
int sum = 0;
string x;
stringstream l(s);
while (l >> x)
{
for (int i = 0; i < 13; i++) if (x == a[i]) sum += i;
for (int i = 1; i < 13; i++) if (x == b[i]) sum += i * 13;
}
cout << sum << endl;
}
}
}