在Stoi函数中,忘记对ans进行初始化,在本地运行没有任何问题,但是提交到oj上就出现了段错误。启发我在做题过程中如果发现本地运行没问题但是oj上出现段错误可以考虑是否是某些变量忘记初始化。
#include<cstdio>
#include<string>
#include<iostream>
#include<map>
using namespace std;
string low[13]={"tret","jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};
string high[13]={"#","tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};
map<string,int> mpl,mph;
void initial()
{
for(int i=0; i<13; i++){
mpl[low[i]] = i;
}
for(int i=1; i<13; i++){
mph[high[i]] = i;
}
}
int Stoi(string s)
{
int ans=0;//一定要初始化,如果不初始化可能本地运行没问题,但是在oj上可能就会造成段错误。
for(int i=0; i<s.size(); i++){
ans=ans*10+s[i]-'0';
}
return ans;
}
int main()
{
initial();
int n;
scanf("%d",&n);
string s1,s2;
for(int i=0; i<n; i++){
cin>>s1;
if(s1[0]>='0'&&s1[0]<='9'){
int a = Stoi(s1);
if(a>=13&&a%13==0){
cout<<high[a/13]<<endl;
}
else{
int b,c;
b = a%13;
if(a/13!=0){
c = a/13;
cout<<high[c]<<" ";
}
cout<<low[b]<<endl;
}
}
else{
char c = getchar();
if(c==' '){
cin>>s2;
cout<<mph[s1]*13+mpl[s2]<<endl;
}
else{
if(mph.count(s1)==1){
cout<<mph[s1]*13<<endl;
}
else cout<<mpl[s1]<<endl;
}
}
}
return 0;
}