PAT1044 火星数字 (20 分)

题目

这题要注意细节。不是很复杂的一道题,但是要考虑一些特殊情况。昨天做了一个多小时没出来正确结果,今天又改了一个多小时,整个代码思路比较混乱

在这里插入图片描述
所有的对应

169
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
13
tam
14
tam jan
15
tam feb
16
tam mar
17
tam apr
18
tam may
19
tam jun
20
tam jly
21
tam aug
22
tam sep
23
tam oct
24
tam nov
25
tam dec
26
hel
27
hel jan
28
hel feb
29
hel mar
30
hel apr
31
hel may
32
hel jun
33
hel jly
34
hel aug
35
hel sep
36
hel oct
37
hel nov
38
hel dec
39
maa
40
maa jan
41
maa feb
42
maa mar
43
maa apr
44
maa may
45
maa jun
46
maa jly
47
maa aug
48
maa sep
49
maa oct
50
maa nov
51
maa dec
52
huh
53
huh jan
54
huh feb
55
huh mar
56
huh apr
57
huh may
58
huh jun
59
huh jly
60
huh aug
61
huh sep
62
huh oct
63
huh nov
64
huh dec
65
tou
66
tou jan
67
tou feb
68
tou mar
69
tou apr
70
tou may
71
tou jun
72
tou jly
73
tou aug
74
tou sep
75
tou oct
76
tou nov
77
tou dec
78
kes
79
kes jan
80
kes feb
81
kes mar
82
kes apr
83
kes may
84
kes jun
85
kes jly
86
kes aug
87
kes sep
88
kes oct
89
kes nov
90
kes dec
91
hei
92
hei jan
93
hei feb
94
hei mar
95
hei apr
96
hei may
97
hei jun
98
hei jly
99
hei aug
100
hei sep
101
hei oct
102
hei nov
103
hei dec
104
elo
105
elo jan
106
elo feb
107
elo mar
108
elo apr
109
elo may
110
elo jun
111
elo jly
112
elo aug
113
elo sep
114
elo oct
115
elo nov
116
elo dec
117
syy
118
syy jan
119
syy feb
120
syy mar
121
syy apr
122
syy may
123
syy jun
124
syy jly
125
syy aug
126
syy sep
127
syy oct
128
syy nov
129
syy dec
130
lok
131
lok jan
132
lok feb
133
lok mar
134
lok apr
135
lok may
136
lok jun
137
lok jly
138
lok aug
139
lok sep
140
lok oct
141
lok nov
142
lok dec
143
mer
144
mer jan
145
mer feb
146
mer mar
147
mer apr
148
mer may
149
mer jun
150
mer jly
151
mer aug
152
mer sep
153
mer oct
154
mer nov
155
mer dec
156
jou
157
jou jan
158
jou feb
159
jou mar
160
jou apr
161
jou may
162
jou jun
163
jou jly
164
jou aug
165
jou sep
166
jou oct
167
jou nov
168
jou dec

代码

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string di[13] = { "tret","jan","feb","mar","apr", "may","jun","jly","aug","sep","oct","nov","dec" };
	string gao[13] = { "","tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou" };

	int total;
	cin >> total;

	int i;
	int j;
	int k;
	string str;
	int result = 0;
	int num = 0;
	int size;
	for (i = 0; i < total; i++)
	{
		cin >> str;
		num = 0;
		result = 0;

		if (str[0] >= '0'&&str[0] <= '9')//是数字
		{
			//转换成数字
			size = str.length();
			for (j = 0; j < size; j++)
			{
				num *= 10;
				num += (str[j] - '0');
			}

			//输出火星文
			if (num >= 13)
			{
				int diwei = num % 13;
				if (num % 13 != 0)cout << gao[num / 13] << ' ';
				if (num % 13 == 0)
				{
					cout << gao[num / 13] << endl;
					continue;
				}
				num /= 13;
				cout << di[diwei] << endl;
				continue;
			}
			else
			{
				cout << di[num] << endl;
				continue;
			}
		}



		else//是火星文
		{
		label1:
			for (j = 0; j < 13; j++)//是高位
			{
				if (str == gao[j])
				{
					result += 13 * j;
					while (cin >> str)//输入低位(注意低位可能不存在)
					{
						for (k = 0; k < 13; k++)//匹配并计算低位
						{
							if (str == di[k])
							{
								result += k;
								goto label;
							}
						}
						if (k == 13)//低位不存在
						{
							i++;
							cout << result << endl;
							result = 0;
							goto label1;
						}
					}
				}
			}

			for (j = 0; j < 13; j++)//是低位
			{
				if (str == di[j])//仅低位
				{
					result += j;
					goto label;
				}
			}

		label:;
			cout << result << endl;
		}
	}

	//system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/sinat_42483341/article/details/87928940
今日推荐