- 做过蓝桥历年题目的就会发现,近年来常考日期相关的题目
- 凭良心说,涉及日期的题目无非就是枚举,不过有时候最基础的东西往往会轻视它,导致失分
·【蓝桥】·跑步锻炼
- 不要想着日历、excel什么取巧的方法,老老实实枚举日期: 2000.1.1~2020.10.1
- 注意闰年的判断、题目的要求
————————————完整代码————————————
int months[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int main(){
int ans = 0;
int yy = 2000, mm = 1, dd = 1;
int temp = 5;
while(yy != 2020 || mm != 10 || dd != 1){
if(yy%400==0 || (yy%4==0 && yy%100!=0)){
months[2] = 29;
}else{
months[2] = 28;
}
dd++;
if(dd > months[mm]){
dd = 1;
mm++;
}
if(mm > 12){
mm = 1;
yy++;
}
temp = (temp + 1) % 7;
if(dd == 1 || temp == 0){
ans++;
}
ans++;
}
printf("%d\n", ans + 2);
//printf("%d\n", temp);
return 0;
}
·【蓝桥】·日期问题
题目链接:·【蓝桥】·日期问题
- 日期相关的题目就是很单一,这道题甚至只用枚举三种情况
- 注意日期升序依次输出,使用set较为方便
————————————完整代码————————————
#include<cstdio>
#include<string>
#include<iostream>
#include<set>
#include<algorithm>
using namespace std;
/*
02/03/04
*/
char s[8];
int months[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
set<string> st;
int toInt(string ss){
int ans = 0;
for(int i = 0; i < ss.length(); ++i){
ans = 10 * ans + (ss[i]-'0');
}
return ans;
}
void solve(string a, string b, string c){
int yy = toInt(a);
if(yy < 60){
yy += 2000;
a = "20" + a;
}else{
yy += 1900;
a = "19" + a;
}
int mm = toInt(b);
int dd = toInt(c);
if(yy%400==0 || (yy%4==0&&yy%100!=0)){ //闰年
months[2] = 29;
}else{
months[2] = 28;
}
if(mm > 12 || mm == 0 || dd > months[mm] || dd == 0){ //合法检验
return;
}
st.insert(a+'-'+b+'-'+c);
//cout << st.size() << endl;
}
int main(){
scanf("%s", &s);
string a(s, 0, 2);
string b(s, 3, 2);
string c(s, 6, 2);
solve(a, b, c);
solve(c, a, b);
solve(c, b, a);
for(set<string>::iterator it = st.begin(); it != st.end(); ++it){
cout << *it <<endl;
}
return 0;
}
·【蓝桥】·会文日期
题目链接:·【蓝桥】·回文日期
- 从n+1这个日期一直遍历到99999999
- 合法日期、闰年的校验和上面一样
- 回文日期、ABABBABA型先后输出,因此需要预先标记一下回文日期
————————————完整代码————————————
#include <cstdio>
#include <string>
#include <iostream>
using namespace std;
int months[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
bool judge_1(string s){ //回文日期
for(int i = 0; i < s.length() / 2; ++i){
if(s[i] != s[s.length()-i-1]){
return false;
}
}
return true;
}
bool judge_2(string s){ //ABABBABA型日期
if(s[0] == s[1] || s[0] != s[2] || s[1] != s[3]){
return false;
}
return judge_1(s);
}
string toStr(int n){ //数值n转为string类型
char ss[10];
sprintf(ss, "%d", n);
return ss;
}
int main(){
int n;
int flag = 0; //标记回文日期是否出现
scanf("%d", &n);
for(int i = n + 1; i <= 99999999; ++i){
int yy = i / 10000, mm = i % 10000 / 100, dd = i % 100;
if(yy%400==0 || (yy%4==0&&yy%100!=0)){
months[2] = 29;
}else{
months[2] = 28;
}
if(mm > 13 || mm == 0 || dd == 0 || dd > months[mm]){
continue;
}
string ss = toStr(i);
if(!flag){
if(judge_1(ss)){
flag = 1;
cout << ss << endl;
if(judge_2(ss)){
cout << ss << endl;
break;
}
}
}else{
if(judge_2(ss)){
cout << ss << endl;
break;
}
}
}
return 0;
}