P8651 [蓝桥杯 2017 省 B] 日期问题

该C++代码定义了一个函数select用于验证给定的年月日是否有效,并在main函数中获取用户输入,遍历1960年至2059年间的日期,输出所有符合闰年规则的日期

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里插入图片描述

#include <iostream>
#include <string>
using namespace std;

int first;
int second;
int third;
int day[13]={0,31,0,31,30,31,30,31,31,30,31,30,31};//每月日期

bool select (int i,int j,int k){
	if ((i%100 == first) && (j == second) && (k == third)) 	//年月日 
		return true;
	if ((i%100 == third) && (j == first) && (k == second))	//月日年 
		return true;
	if ((i%100 == third) && (j == second) && (k == first))  //日月年 
		return true;
	return false;
}
int main(){
	string s;
	cin>>s;
	getchar();
	first  = ((int)s[0] - 48)* 10 + ((int)s[1] - 48);
	second = ((int)s[3] - 48)* 10 + ((int)s[4] - 48);
	third  = ((int)s[6] - 48)* 10 + ((int)s[7] - 48);
	for (int i = 1960; i <= 2059; i++){
		if ((i % 4 == 0 && i % 100 != 0) || (i % 400 == 0)) 
			day[2] = 29;
		else 
			day[2] = 28;
		for (int j = 1;j <= 12;j++){
			for (int k = 1;k <= day[j];k++){
				if (select(i,j,k)){
				 	if (i % 100 < 10)	cout<< "200" << i % 100 << "-";
					else if (i % 100 < 60) cout<< "20" << i % 100 << "-";
					else cout<< "19"<<i % 100<<"-";
					
					if	(j < 10) cout<< "0"<< j <<"-";
					else	cout<< j <<"-";
					
					if	(k < 10) cout<< "0"<<k<<endl;
					else	cout<< k<<endl;
				 } 
			} 
		}	
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值