0%

PAT A1100 Mars Numbers

题目链接

全map解法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <cstring>
#include <cstdio>
#include <map>
#include <iostream>
#include <string>

using namespace std;
map<int, string> ge = {{0, "tret"},
{1, "jan"},
{2, "feb"},
{3, "mar"},
{4, "apr"},
{5, "may"},
{6, "jun"},
{7, "jly"},
{8, "aug"},
{9, "sep"},
{10, "oct"},
{11, "nov"},
{12, "dec"}};
map<int, string> shi = {{1, "tam"},
{2, "hel"},
{3, "maa"},
{4, "huh"},
{5, "tou"},
{6, "kes"},
{7, "hei"},
{8, "elo"},
{9, "syy"},
{10, "lok"},
{11, "mer"},
{12, "jou"}};
map<string, int> shi2 = {{"tam", 1},
{"hel", 2},
{"maa", 3},
{"huh", 4},
{"tou", 5},
{"kes", 6},
{"hei", 7},
{"elo", 8},
{"syy", 9},
{"lok", 10},
{"mer", 11},
{"jou", 12}};
map<string, int> ge2 = {{"tret", 0},
{"jan", 1},
{"feb", 2},
{"mar", 3},
{"apr", 4},
{"may", 5},
{"jun", 6},
{"jly", 7},
{"aug", 8},
{"sep", 9},
{"oct", 10},
{"nov", 11},
{"dec", 12}};

void tenTo13(int x) {
int sh = x / 13;
if (sh != 0) {
int gew = x % 13;
if (gew != 0) {
printf("%s %s\n", shi[sh].c_str(),ge[gew].c_str());
}else{
printf("%s\n",shi[sh].c_str());
}
} else {
sh = x % 13;
printf("%s\n", ge[sh].c_str());
}

}

int main() {
int n;
string s, s1, s2;
scanf("%d", &n);
getchar();
for (int i = 0; i < n; i++) {
getline(cin, s);
if (isdigit(s[0])) {
tenTo13(stoi(s));
} else {
int len = s.length();
if (len == 3) {
if (ge2.count(s) != 0) printf("%d\n", ge2[s]);
else printf("%d\n", shi2[s] * 13);
} else if (len == 4) {
printf("0\n");
} else {
s1 = s.substr(0, 3);
s2 = s.substr(4);
printf("%d\n", shi2[s1] * 13 + ge2[s2]);
}
}
}

return 0;
}

打表法

因为数的范围是从0~169,13进制的数,最多为两位,所以可以直接打表来做。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <cstring>
#include <cstdio>
#include <map>
#include <iostream>
#include <string>
using namespace std;
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"};

string numtostr[170];
map<string,int> strtonum;
void init(){
//只火星为考虑一位
for(int i=0;i<13;i++){
numtostr[i]=ge[i];
strtonum[ge[i]]=i;
numtostr[i*13]=shi[i];
strtonum[shi[i]]=i*13;
}
//考虑;两位
for(int i=1;i<13;i++){
for(int j=1;j<13;j++){
string s=shi[i]+" "+ge[j];
numtostr[i*13+j]=s;
strtonum[s]=i*13+j;
}
}

}

int main() {
init();
int n;
string s;
scanf("%d", &n);
getchar();
for (int i = 0; i < n; i++) {
getline(cin, s);
if (isdigit(s[0])) {
printf("%s\n",numtostr[stoi(s)].c_str());
} else {
printf("%d\n",strtonum[s]);
}
}

return 0;
}

本文标题:PAT A1100 Mars Numbers

文章作者:GavinYGM

发布时间:2020年09月12日 - 11:09

最后更新:2020年09月12日 - 11:09

原始链接:http://www.gavinygm.cn/2020/09/12/PAT-A1100-Mars-Numbers/

许可协议: 转载请保留原文链接及作者。