目录
1,题目描述
Sample Input:
4
29
5
elo nov
tam
Sample Output:
hel mar
may
115
13
题目大意
将数字(地球表示法)与13进制的字符串(火星表示法)相互转换;
2,思路
涉及两个函数:
toMars(数字转换为13进制相应字符串):
toEarth(13进制相应字符串转换为数字):
注意
- 使用getline函数时,一定要将上一行末尾的\n提前接受掉,不然会将\n作为第一行;
- 13的火星表示是tam,而不是tem tret;
3,AC代码
#include<bits/stdc++.h>
using namespace std;
string high[13] = {"tret", "tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};
string low[13] = {"tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};
void toMars(string s){
int h, l, num;
num = stoi(s);
h = num / 13;
l = num % 13;
if(h != 0){
printf("%s", high[h].c_str());
if(l != 0)
printf(" %s", low[l].c_str());
}else
printf("%s", low[l].c_str());
printf("\n");
}
void toEarth(string s){
int i = 0, num1, num2, ans = 0;
string s1, s2;
while(i < s.size() && s[i] != ' ') i++;
if(i < s.size()){
s1 = s.substr(0, i);
for(int j = 0; j < 13; j++)
if(high[j] == s1){
ans += j * 13;
break;
}
s2 = s.substr(i + 1);
for(int j = 0; j < 13; j++)
if(low[j] == s2){
ans += j;
break;
}
}else{
for(int j = 0; j < 13; j++){
if(high[j] == s){
ans += j * 13;
break;
}
if(low[j] == s){
ans += j;
break;
}
}
}
printf("%d\n", ans);
}
int main(){
#ifdef ONLINE_JUDGE
#else
freopen("1.txt", "r", stdin);
#endif // ONLINE_JUDGE
string s;
int N;
scanf("%d\n", &N); //注意加上\n 不然getline函数接受的第一行是\n
for(int i = 0; i < N; i++){
getline(cin, s);
if(isdigit(s[0]))
toMars(s);
else
toEarth(s);
}
return 0;
}
4,解题过程
第一搏
#include<bits/stdc++.h>
using namespace std;
string high[13] = {"tret", "tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};
string low[13] = {"tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};
void toMars(string s){
int h, l, num;
num = stoi(s);
h = num / 13;
l = num % 13;
if(h != 0) printf("%s ", high[h].c_str());
printf("%s\n", low[l].c_str());
}
void toEarth(string s){
int i = 0, num1, num2, ans = 0;
string s1, s2;
while(i < s.size() && s[i] != ' ') i++;
if(i < s.size()){
s1 = s.substr(0, i);
//cout<<"s1:"<<s1<<endl;
for(int j = 0; j < 13; j++)
if(high[j] == s1){
ans += j * 13;
break;
}
s2 = s.substr(i + 1);
//cout<<"s2:"<<s2<<endl;
for(int j = 0; j < 13; j++)
if(low[j] == s2){
ans += j;
break;
}
}else{
for(int j = 0; j < 13; j++){
if(high[j] == s){
ans += j * 13;
break;
}
if(low[j] == s){
ans += j;
break;
}
}
}
printf("%d\n", ans);
}
int main(){
#ifdef ONLINE_JUDGE
#else
freopen("1.txt", "r", stdin);
#endif // ONLINE_JUDGE
string s;
int N;
scanf("%d\n", &N);//注意加上\n 不然getline函数第一个接受的是\n
for(int i = 0; i < N; i++){
getline(cin, s);
//cout<<s<<endl;
//if(s[s.size()-1] == '\n') cout<<"YES";
if(isdigit(s[0]))
toMars(s);
else
toEarth(s);
}
return 0;
}
第二搏
注意到
13的火星表示是tam,而不是tem tret。于是进行了以下修改: