蓝桥杯每日一题2023.11.8

题目描述

题目分析

对于输入的abc我们可以以a为年也可以以c为年,将abc,cab,cba这三种情况进行判断合法性即可,注意需要排序去重,所以考虑使用set

此处为纯模拟的写法,但使用循环代码会更加简洁。

方法一:

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
set<string> st;
string s[N];
int cnt;
int m[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
bool is_ren(int x)
{
	if((x % 4 == 0 && x % 100 != 0)||(x % 400 == 0))return true;
	return false;
}
int main()
{
	int a, b, c;
	scanf("%d/%d/%d",&a, &b, &c);
	//以a为年 //abc
	if(a <= 59)
	{
		//abc
		if(b <= 12 && b > 0)
		{
			int x = 2000 + a;
			if(is_ren(x))m[2] = 29;
			else m[2] = 28;
			if(c <= m[b] && c > 0)
			{
				cnt ++;
				s[cnt] = "20";
				if(a < 10)s[cnt] += "0";
				s[cnt] += (to_string(a) + "-" );
				if(b < 10)s[cnt] += "0";
				s[cnt] += (to_string (b) + "-");
				if(c < 10)s[cnt] +="0";
				s[cnt] += to_string(c);
				st.insert(s[cnt]);
			}
		}
	}
 	else if(a > 59)
	{
		//abc
		if(b <= 12 && b > 0)
		{
			int x = 1900 + a;
			if(is_ren(x))m[2] = 29;
			else m[2] = 28;
			if(c <= m[b] && c > 0)
			{
				cnt ++;
				s[cnt] = "19";
				if(a < 10)s[cnt] += "0";
				s[cnt] += to_string(a) + "-" ;
				if(b < 10)s[cnt] += "0";
				s[cnt] += to_string (b) + "-";
				if(c < 10)s[cnt] +="0";
				s[cnt] += to_string(c);
				st.insert(s[cnt]);
			}
		
		}
	}
	//以c为年 //cab, cba 
	if(c <= 59)
	{
		//cab
		if(a <= 12 && a > 0)
		{
			int x = 2000 + c;
			if(is_ren(x))m[2] = 29;
			else m[2] = 28;
			if(b <= m[a] && b > 0)
			{
				s[++ cnt] = "20";
				if(c < 10)s[cnt] += "0";
				s[cnt] += to_string(c) + "-" ;
				if(a < 10)s[cnt] += "0";
				s[cnt] += to_string (a) + "-";
				if(b < 10)s[cnt] +="0";
				s[cnt] += to_string(b);
				st.insert(s[cnt]);
			}
		
		}
		//cba
		if(b <= 12 && b > 0)
		{
			int x = 2000 + c;
			if(is_ren(x))m[2] = 29;
			else m[2] = 28;
			if(a <= m[b] && a > 0)
			{
				cnt ++;
				s[cnt] = "20";
				if(c < 10)s[cnt] += "0";
				s[cnt] += to_string(c) + "-" ;
				if(b < 10)s[cnt] += "0";
				s[cnt] += to_string (b) + "-"; 
				if(a < 10)s[cnt] +="0";
				s[cnt] += to_string(a);
				st.insert(s[cnt]);
			}	
		}
	}
	else if(c > 59)
	{
		//cab
		if(a <= 12 && a > 0)
		{
			int x = 1900 + c;
			if(is_ren(x))m[2] = 29;
			else m[2] = 28;
			if(b <= m[a] && b > 0)
			{
				cnt ++;
				s[cnt] = "19";
				if(c < 10)s[cnt] += "0";
				s[cnt] += to_string(c) + "-" ;
				if(a < 10)s[cnt] += "0";
				s[cnt] += to_string (a) + "-";
				if(b < 10)s[cnt] +="0";
				s[cnt] += to_string(b);
				st.insert(s[cnt]);
			}
			
		}
		//cba
		if(b <= 12 && b > 0)
		{
			int x = 1900 + c;
			if(is_ren(x))m[2] = 29;
			else m[2] = 28;
			if(a <= m[b] && a > 0)
			{
				cnt ++;
				s[cnt] = "20";
				if(c < 10)s[cnt] += "0";
				s[cnt] += to_string(c) + "-" ;
				if(b < 10)s[cnt] += "0";
				s[cnt] += to_string (b) + "-";
				if(a < 10)s[cnt] +="0";
				s[cnt] += to_string(a);
				st.insert(s[cnt]);
			}
		}
	}
	for(auto i : st)
	{
		cout << i << '\n';
	}
	return 0;
}

方法二:

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
int days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
bool check_valid(int year, int month, int day)
{
	if(month == 0 || month > 12)return false;
	if(day == 0)return false;
	if(month != 2)
	{
		if(day > days[month])return false;
	}
	else
	{
		int leap = year % 100 && year % 4 == 0 || year % 400 == 0;
		if(day > 28 + leap)return false;
	}
	return true;
}
int main()
{
	int a, b, c;
	scanf("%d/%d/%d", &a, &b, &c);
	for(int date = 19600101; date <= 20591231; date ++)
	{
		int year = date / 10000, month = date % 10000 / 100, day = date % 100;
		if(check_valid(year, month, day))
		{
			if(year % 100 == a && month == b && day == c ||
			   month == a && day == b && year % 100 == c ||
			   day == a && month == b && year % 100 == c)
			printf("%d-%02d-%02d\n", year, month, day);
		}
	}
	return 0;
}
相关推荐
快手技术6 分钟前
从“拦路虎”到“修路工”:基于AhaEdit的广告素材修复
前端·算法·架构
qk学算法7 分钟前
力扣滑动窗口题目-76最小覆盖子串&&1234替换子串得到平衡字符串
数据结构·算法·leetcode
小欣加油8 分钟前
leetcode 860 柠檬水找零
c++·算法·leetcode·职场和发展·贪心算法
lijun_xiao200929 分钟前
.net 面试题目
面试·职场和发展
粉色挖掘机1 小时前
矩阵在密码学的应用——希尔密码详解
线性代数·算法·机器学习·密码学
七七七七071 小时前
【计算机网络】UDP协议深度解析:从报文结构到可靠性设计
服务器·网络·网络协议·计算机网络·算法·udp
TitosZhang2 小时前
排序算法稳定性判断
数据结构·算法·排序算法
一种乐趣2 小时前
PHP推荐权重算法以及分页
算法·php·推荐算法
ccLianLian3 小时前
计算机视觉·TagCLIP
人工智能·算法
千弥霜3 小时前
codeforces1997(div.3)E F
算法