当前位置: 首页 > article >正文

P8651 [蓝桥杯 2017 省 B] 日期问题--注意日期问题中2月的天数 / if是否应该连用

P8651 [P8651 [蓝桥杯 2017 省 B] 日期问题--注意日期问题中2月的天数 / if是否应该连用

      • 题目
  • 分析
      • 代码

题目

在这里插入图片描述

分析

代码中巧妙的用到3重循环,完美的解决了输出的顺序问题【题目要求从小到大】

需要注意的是2月的值,在不同的年份中应该更新2月的值

还有就是最后的3个if【T^T,我一开始写的3个if语句】,要么改成else if,要么把判定的条件改成用“||”连接【害我看半天T^T】,不然用3个if回导致重复输出

代码

#include <iostream>
#include <vector>
#include <set>
#include <string>
#include <algorithm>
#include <math.h>
#include <queue>
#include <climits>  // 包含INT_MAX常量
#include <cctype>
using namespace std;

int month[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int check(int x) {
	return (x % 4 == 0 && x % 100 != 0) || (x % 400 == 0);
}

int main() {
	int a, b, c;
	scanf("%d/%d/%d", &a, &b, &c);
	//循环从早到晚,巧妙满足了题目要求的从早到晚排列
	for (int year = 1960; year <= 2059; year++) {
		if (check(year))
			month[2] = 29;
		else
			month[2] = 28;
		for (int mth = 1; mth <= 12; mth++) {
			for (int day = 1; day <= month[mth]; day++) {
				if (a == year % 100 && b == mth && day == c)
					printf("%d-%02d-%02d\n", year, mth, day);
				else if (a == mth && b == day && c == year % 100 )
					printf("%d-%02d-%02d\n", year, mth, day);
				else if (a == day && b == mth &&  c == year % 100 )
					printf("%d-%02d-%02d\n", year, mth, day);
			}
		}
	}
	return 0;
}



http://www.kler.cn/a/569779.html

相关文章:

  • 深度学习-10.门控循环网络
  • 《STL源码剖析》笔记记录
  • Unity中动态切换光照贴图LightProbe的方法
  • 服务流程设计和服务或端口重定向及其websocket等应用示例
  • 《Python百练成仙》31-40章(不定时更新)
  • iOS UICollectionViewCell 点击事件自动化埋点
  • react 中,使用antd layout布局中的sider 做sider的展开和收起功能
  • 【Linux】进程地址空间和虚拟地址空间
  • 测试的BUG分析
  • 42 session反序列化漏洞
  • 数据结构(初阶)(七)----树和二叉树(前中后序遍历)
  • vcredist_x64 资源文件分享
  • React(11)路由demo
  • IDEA相关配置
  • LeetCode-154. 寻找旋转排序数组中的最小值 II
  • go类(结构体)和对象
  • 派可数据BI接入DeepSeek,开启智能数据分析新纪元
  • GPT-4.5
  • 力扣785. 判断二分图
  • 黑龙江省地标-DB31/T 862-2021 “一网通办”政务服务中心建设和运行规范