第一遍做,没看懂题目真正意思,导致3个测试点没过,没看懂题目真要命。
//自以为For the next higher digit, Mars people name the 12 numbers as “tam, hel, maa, huh, tou, kes, hei, elo, syy, lok, mer, jou”, respectively.的意思是:12个数指13-24
实际意思为:火星人将进位以后的12个高位数字分别称为:“tam, hel, maa, huh, tou, kes, hei, elo, syy, lok, mer, jou”
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<map>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main(){
#ifdef ONLINE_JUDGE
#else
freopen("1.txt","r",stdin);
#endif
map<string,int> cd;
map<int,string> dc;
char ch[25][5]={"tret","jan","feb","mar","apr","may","jun","jly","aug","sep","oct","nov","dec","tam","hel","maa","huh","tou","kes","hei","elo","syy","lok","mer","jou"};
for(int i=0;i<25;i++){
cd[ch[i]]=i;
dc[i]=ch[i];
}
int n;
scanf("%d",&n);
getchar();
for(int i=0;i<n;i++){
string inp;
getline(cin,inp);
if(inp[0]>='0'&&inp[0]<='9'){
int d;
sscanf(inp.c_str(),"%d",&d);
if(d<=24)printf("%s\n",dc[d].c_str());
else printf("%s %s\n",dc[12+(d/13)].c_str(),dc[d%13].c_str());
}else{
int l=inp.length();
if(l<=5)printf("%d\n",cd[inp]);
else{
int ind=inp.find(" ");
printf("%d\n",(cd[inp.substr(0,ind)]-12)*13+cd[inp.substr(ind+1,l-ind-1)]);
}
}
}
return 0;
}
看了算法笔记思路:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<map>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
string ds[169];
map<string,int> sd;
string ge[13]={"tret","jan","feb","mar","apr","may","jun","jly","aug","sep","oct","nov","dec"};
string shi[13]={"tret","tam","hel","maa","huh","tou","kes","hei","elo","syy","lok","mer","jou"};
void init(){ //建表
for(int i=0;i<13;i++){ //个位0-12,十位0;十位0-12,个位0
ds[i]=ge[i];
sd[ge[i]]=i;
ds[i*13]=shi[i];
sd[shi[i]]=i*13;
}
for(int i=1;i<13;i++){ //个位1-12,十位1-12
for(int j=1;j<13;j++){
ds[i*13+j]=shi[i]+" "+ge[j];
sd[ds[i*13+j]]=i*13+j;
}
}
}
int main(){
#ifdef ONLINE_JUDGE
#else
freopen("1.txt","r",stdin);
#endif
init();
int n;
scanf("%d",&n);
getchar();
while(n--){
string str;
getline(cin,str);
if(str[0]>='0'&&str[0]<='9'){
int a;
sscanf(str.c_str(),"%d",&a);
cout<<ds[a]<<endl;
}else{
cout<<sd[str]<<endl;
}
}
return 0;
}
做了本题发现对进制转换还是没能很好的理解,这篇博客讲的较为详细:进制转换方法总结——摘自百度
本题最值得注意的地方,能被13整除的数,其个位无需输出,只需输出进位进上去的十位所对应的字符串即可。(其实样例中已有给出例子,但当时对进制转换不熟悉,导致没有想到)