七夕了呀,那就刷题吧~~~
本次试题依旧使用 C++ 进行求解~由于疫情原因,本次PAT由原来的线下举办改为线上考试~ 9月份还有一场,想参加的小伙伴不要错过呀~
由于本人过菜,这次七夕教超打折依旧只敢去做做乙级题(截至目前已售出30份)~😂
废话不多说,开冲!
PAT(乙级)2020年春季考试直达链接:这里
试题集の基本情况
对称日(15分)
思路
- 需要处理输入的 date
- 将月份应为简写转换为对应的数字
- 将年日月按要求组合起来
- 检验是否为对称日
- 可以转换为字符串,按对应的位或者反转进行对比
- 可以将月份与日期的和(月份 * 100 + 日期)反转,与年份对比
这里我使用了 map 作为月份(英文 - 数字)的映射、 sprintf 格式化字符串接着按位比较~ 太香啦~~(由于太菜,使用其他方法补零补到崩溃,导致做了半个小时才AC的屑博主)
#include <cstdio>
#include <algorithm>
#include <unordered_map>
#include <string>
using std::string;
using std::unordered_map;
int main() {
unordered_map<string, int> um{
{
"Jan", 1},
{
"Feb", 2},
{
"Mar", 3},
{
"Apr", 4},
{
"May", 5},
{
"Jun", 6},
{
"Jul", 7},
{
"Aug", 8},
{
"Sep", 9},
{
"Oct", 10},
{
"Nov", 11},
{
"Dec", 12}
};
int cnt{
0 }, year{
0 }, day{
0 };
char month[5];
scanf("%d", &cnt);
getchar();
for (int i = 0; i < cnt; i++) {
scanf("%s %d, %d", month, &day, &year);
char date[10];
sprintf(date, "%04d%02d%02d", year, um[string(month)], day);
int left = 0, right = 7;
while (left < right) {
if (date[left] != date[right]) {
putchar('N');
break;
}
left++;
right--;
}
if (left > right) {
putchar('Y');
}
printf(" %04d%02d%02d\n", year, um[string(month)], day