字符串处理
#include <iostream>
#include <vector>
using namespace std;
string strs1[12]={"tam","hel","maa","huh","tou","kes",
"hei","elo",
"syy","lok","mer","jou"};
string strs2[13]={"tret","jan","feb","mar","apr","may","jun","jly",
"aug","sep","oct","nov","dec"};
string findNum(string& s){
if(isdigit(s[0])){
int num=stoi(s);
int a=num/13;
int b=num%13;
if(a==0&&b==0){
return strs2[b];
} else if(a==0&&b!=0){
return strs2[b];
}else if(a!=0&&b==0){
return strs1[a-1];
} else{
return strs1[a-1]+" "+strs2[b];
}
} else{
if (s.find(' ')==string::npos){
for (int i = 0; i < 13; ++i) {
if(strs2[i]==s){
return to_string(i);
}
}
for (int j = 0; j < 12; ++j) {
if(strs1[j]==s){
return to_string(13*(j+1));
}
}
} else{
int index=s.find(' ');
string s1=s.substr(0,index);
string s2=s.substr(index+1,s.length()-index);
int a=0,b=0;
for (int i = 0; i < 12; ++i) {
if(strs1[i]==s1){
a=i+1;
break;
}
}
for (int j = 0; j < 13; ++j) {
if(s2==strs2[j]){
b=j;
break;
}
}
return to_string(a*13+b);
}
}
}
int main() {
int N;
cin>>N;
cin.get();
vector<string> vector1;
for (int i = 0; i < N; ++i) {
string s;
getline(cin,s);
vector1.push_back(findNum(s));
}
for(auto x:vector1){
cout<<x<<endl;
}
return 0;
}
二刷:
#include <iostream>
#include<vector>
using namespace std;
string array1[13]={"tret","jan","feb","mar","apr","may","jun",
"jly","aug","sep","oct","nov","dec"
};
string array2[12]={"tam","hel","maa","huh","tou","kes","hei","elo",
"syy","lok","mer","jou"
};
string fun1(string s1){
int val=stoi(s1);
int a=val/13;
int b=val%13;
string res="";
if(a!=0){
res+=array2[a-1];
if(b==0){
return res;
}
res+=" ";
}
res+=array1[b];
return res;
}
string fun2(string s1){
int res=0;
if(s1.find(' ')==string::npos){
for(int i=0;i<13;i++){
if(array1[i]==s1){
return to_string(i);
}
}
for(int i=0;i<12;i++){
if(array2[i]==s1){
return to_string((i+1)*13);
}
}
}else{
int index=s1.find(' ');
string s2=s1.substr(0,index);
string s3=s1.substr(index+1);
for(int i=0;i<13;i++){
if(array1[i]==s3){
res+=i;
}
}
for(int i=0;i<12;i++){
if(array2[i]==s2){
res+=((i+1)*13);
}
}
}
return to_string(res);
}
int main()
{
int N;
cin>>N;
getchar();
vector<string> vector1;
for(int i=0;i<N;i++){
string s1;
// cin>>s1;
getline(cin,s1);
if(isdigit(s1[0])){
vector1.push_back(fun1(s1));
}else{
vector1.push_back(fun2(s1));
}
}
for(int i=0;i<vector1.size();i++){
cout<<vector1[i]<<endl;
}
return 0;
}