【蓝桥杯AcWing】专题———日期(重点)

  • 做过蓝桥历年题目的就会发现,近年来常考日期相关的题目
  • 凭良心说,涉及日期的题目无非就是枚举,不过有时候最基础的东西往往会轻视它,导致失分

·【蓝桥】·跑步锻炼

在这里插入图片描述

  • 不要想着日历、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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值