[蓝桥杯历届试题] 国庆星期日

22 篇文章 0 订阅

1949年的国庆节(10月1日)是星期六。

今年(2012)的国庆节是星期一。

那么,从建国到现在,有几次国庆节正好是星期日呢?

只要答案,不限手段!

可以用windows日历,windows计算器,Excel公式,。。。。。

当然,也可以编程!

不要求写出具体是哪些年,只要一个数目!

千万不要提交源代码!

答案不要写在这里,写在“解答.txt”中

 

参考答案:

9

这题查日历貌似是最快的方法吧。编程序花的时间肯定比查日历的时间长。不过我还是编了程序检验一下。

解题思路:一天一天的加。日期加,星期加。


代码:

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
using namespace std;

bool leap(int year)
{
	if(year%4==0&&year%100!=0||year%400==0)
		return true;
	return false;
}
int main(int argc, char *argv[]) {
	
	int year=1949,month=10,day=1;
	int th=6;//星期几 
	int count=0;
	for(int i=1;i<=23000;i++)
	{
		day++;
		th++;
		if(th>7)
		th=1;
		if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
		{
			if(day>31)
			{
				month++;
				day=1;
			}
		} 
		if(month==4||month==6||month==9||month==11)
		{
			if(day>30)
			{
				month++;
				day=1;
			}
		}
		if(month==2&&leap(year))
		{
			if(day>29)
			{
				month++;
				day=1;
			}
		}
		else if(month==2&&!leap(year))
		{
			if(day>28)
			{
				month++;
				day=1;
			}
		}
		if(month>12)
		{
			year++;
			month=1;
		}
		if(month==10&&day==1&&th==7)
		count++;
		if(year==2012)
		break;
	}
	cout<<count<<endl;
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值