题解:P5690 [CSP-S 2019 江西] 日期

513 字
3 分钟
题解:P5690 [CSP-S 2019 江西] 日期

先读题

这题说实话并不难,但是细节真的超级超级多

正解#

我们很容易就可以发现,对于不符合要求的月份或日期,做多只需更改一位(即十位或百位)

那么日期就很好处理了,如果日期大于这个月份的天数,直接ans++即可

难点在于怎么正确处理不符合的月份

极为明确的是,如果我们在月份处理后这个月份的天数尽可能大,就会少一些不必要去变动日期的情况

例:

input: output: realoutput:
22-30 1 2

很明显,把月份2222变成1212可以比变成22多出几天不用变日期的情况,虽然很少,但决定了本题是否能AC

所以理清一下我们的处理思路:

1.先处理月份,对于等于00的情况,随便设置为一个有3131天的月份即可;对于1MM121 \leq MM \leq 12的情况,无需变动

2.对于12<MM12 < MM的情况,分类讨论,如果MMMM的个位大于22,把十位设为00即可;如果个位为00,将MMMM设为1010;如果个位为11,将MMMM设为11(因为11月天数比1111月天数多);如果个位为22,将MMMM设为1212(因为1212月天数比22月天数多)

3.在处理完月份后,如果日期大于这个月份的天数,直接ans++

4.输出ans即可

ACcodeAC code:#

#include<iostream>
#include<string>
#include<string.h>
#include<vector>
#include<queue>
#include<map>
#include<stack>
#include<set>
#include<functional>
#include<utility>
#include<algorithm>
#include<cmath>
#include<climits>
#include<tuple>
#include<numeric>
#include<any>
#include<bitset>
#define int long long
using namespace std;
const int N = 1e5 + 10, M = 31;
int n, m, ans;
char tmp;
vector <int> _31ds;
signed main() {
cin.tie(nullptr)->ios::sync_with_stdio(false);
_31ds.push_back(1);
_31ds.push_back(3);
_31ds.push_back(5);
_31ds.push_back(7);
_31ds.push_back(8);
_31ds.push_back(10);
_31ds.push_back(12);
cin >> n >> tmp >> m;
if (n == 0) n = 8, ans++;
if (n > 12) {
ans++;
if (n % 10 > 2) n %= 10;
else if (n % 10 == 1) n = 1;
else if (n % 10 == 2) n = 12;
else if (n % 10 == 0) n = 10;
}
if (n == 2) ans += m > 28;
else if(find(_31ds.begin(),_31ds.end(),n)!=_31ds.end()) ans += m > 31;
else ans += m > 30;
cout << ans;
return 0;
}

支持与分享

如果这篇文章对你有帮助,欢迎分享给更多人或打赏支持!

打赏
题解:P5690 [CSP-S 2019 江西] 日期
https://azx.xn--0iv.gay/posts/solution-p5690/
作者
WanFoxAZX
发布于
2026-08-25
许可协议
CC BY-NC-SA 4.0

评论区

Profile Image of the Author
WanFoxAZX
Hello, I'm AZX.
公告
Welcome!
分类
标签
最新动态

还没有发布动态

更多动态
站点统计
文章
10
动态
0
分类
6
标签
5
总字数
2,549
运行时长
0
最后活动
0 天前
站点信息
构建平台
Netlify CI
博客版本
Firefly v6.15.5
文章许可
CC BY-NC-SA 4.0