博客
关于我
日期类例题剖析
阅读量:817 次
发布时间:2019-03-25

本文共 1786 字,大约阅读时间需要 5 分钟。

关于日期处理的常用方法,掌握闰年的计算方式和各月的天数即可满足大部分需求。

例题剖析

回文日期

2020年蓝桥杯A组

样例输入:20200202

样例输出:2021120221211212

代码解析:#include

#include
using namespace std;

int days[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};bool isLeap(int year) {return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);}int getDay(int year, int month) {if (month == 2) return days[2] + isLeap(year);return days[month];}int main() {int n;cin >> n;string ans1, ans2;bool flag1 = false, flag2 = false;

for (int i = n / 10000 + 1; i <= 9999; i++) {    string a = to_string(i);    string b = a;    reverse(b.begin(), b.end());    int month = stoi(b.substr(0, 2));    int day = stoi(b.substr(2, 2));    if (month < 1 || month > 12) continue;    if (day < 1 || day > getDay(i, month)) continue;    string s1 = a.substr(0, 2);    string s2 = a.substr(2, 2);    if (!flag1) {        ans1 = a + b;        flag1 = true;    }    if (!flag2 && s1 == s2 && s1[0] != s1[1]) {        ans2 = a + b;        flag2 = true;    }    if (flag1 && flag2) break;}cout << ans1 << endl;cout << ans2 << endl;

}

跑步锻炼

2020年蓝桥杯B题

代码解析:#include

using namespace std;

int days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};bool isLeap(int year) {return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);}int getDay(int year, int month) {if (month == 2) return days[2] + isLeap(year);return days[month];}

int main() {int ans = 0, weekday = 4;for (int i = 2000; i < 2020; i++) {for (int j = 1; j <= 12; j++) {for (int k = 1; k <= getDay(i, j); k++) {weekday = (weekday + 1) % 7;if (k == 1 || weekday == 0) ans += 2;else ans++;}}}for (int j = 1; j < 10; j++) {for (int k = 1; k <= getDay(2020, j); k++) {weekday = (weekday + 1) % 7;if (k == 1 || weekday == 0) ans += 2;else ans++;}}ans += 2; // 2020.10.1cout << ans << endl;}

答案: 8879

转载地址:http://mbayk.baihongyu.com/

你可能感兴趣的文章
NIO笔记---上
查看>>
NIO蔚来 面试——IP地址你了解多少?
查看>>
NISP一级,NISP二级报考说明,零基础入门到精通,收藏这篇就够了
查看>>
NISP国家信息安全水平考试,收藏这一篇就够了
查看>>
NIS服务器的配置过程
查看>>
NIS认证管理域中的用户
查看>>
Nitrux 3.8 发布!性能全面提升,带来非凡体验
查看>>
NiuShop开源商城系统 SQL注入漏洞复现
查看>>
NI笔试——大数加法
查看>>
NLog 自定义字段 写入 oracle
查看>>
NLog类库使用探索——详解配置
查看>>
NLP 基于kashgari和BERT实现中文命名实体识别(NER)
查看>>
NLP 模型中的偏差和公平性检测
查看>>
Vue3.0 性能提升主要是通过哪几方面体现的?
查看>>
NLP 项目:维基百科文章爬虫和分类【01】 - 语料库阅读器
查看>>
NLP_什么是统计语言模型_条件概率的链式法则_n元统计语言模型_马尔科夫链_数据稀疏(出现了词库中没有的词)_统计语言模型的平滑策略---人工智能工作笔记0035
查看>>
NLP三大特征抽取器:CNN、RNN与Transformer全面解析
查看>>
NLP学习笔记:使用 Python 进行NLTK
查看>>
NLP度量指标BELU真的完美么?
查看>>
NLP的不同研究领域和最新发展的概述
查看>>