蓝桥杯VIP——黑色星期五

版权声明:[email protected] https://blog.csdn.net/lytwy123/article/details/84765132

1.问题描述


2.算法分析

典型的日期计算,其实就是枚举思想,首先我们确定从1998.1.1日周四开始枚举,然后判断该年是否为闰年,再去判断当星期数为8时我们需要将星期数重置为星期1开始,当每天的天数等于月末+1天时重置天数为下个月1号,当月份为13时跳出循环,年份+1,再依次循环。最后输出即可。

提示:
这里有几个坑:首先你判断月末和星期数都要+1而不是星期7就变为1,而且月末+1天也是个坑
然后需要注意的是,一开始循环我们不应该先将天数和星期数++,而是在后面进行++
应该先判断是否是黑色星期五在判断星期数,在判断月份是否到年末了。


3.源代码:

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

int m[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};//存储每一个月的天数

int main(){
	int year;
	scanf("%d", &year);
	int y = 1998;
	int d = 1;	//1998.1.1
	int month = 1; //1月份 
	int w = 4;	//周四
	int cnt = 0;  //记录每一年的既是13号又是周五的天数 
	while(y <= year){
//		cnt = 0;  //记录每一年的既是13号又是周五的天数 
		if (y % 400 == 0 || (y % 100 != 0 && y % 4 == 0)){
			m[2] = 29;
		} else{
			m[2] = 28;
		}
		while(1){
			if (d == 13 && w == 5 && y == year){
				cnt++;
			}
			if (w == 8){
				w = 1;
			}
			if (d == m[month] + 1){
				month++;
				d = 1;
			}
			if (month == 13){
				d = 1;
				month = 1;
				break;
			}
			d++;
			w++;
		}
		y++;
	}
	printf("%d\n", cnt); 
	return 0;
} 

可以关注一下Blog:http://47.107.118.184

猜你喜欢

转载自blog.csdn.net/lytwy123/article/details/84765132