蓝桥杯 黑色星期五

问题描述
  有些西方人比较迷信,如果某个月的13号正好是星期五,他们就会觉得不太吉利,用古人的说法,就是“诸事不宜”。请你编写一个程序,统计出在某个特定的年份中,出现了多少次既是13号又是星期五的情形,以帮助你的迷信朋友解决难题。
  说明:(1)一年有365天,闰年有366天,所谓闰年,即能被4整除且不能被100整除的年份,或是既能被100整除也能被400整除的年份;(2)已知1998年1月1日是星期四,用户输入的年份肯定大于或等于1998年。
  输入格式:输入只有一行,即某个特定的年份(大于或等于1998年)。
  输出格式:输出只有一行,即在这一年中,出现了多少次既是13号又是星期五的情形。
输入输出样例
样例输入
1998
样例输出
3

#include <iostream>
#include <string>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
bool isrn(int n){
	if(n%4==0&&n%100!=0||n%400==0){
		return true;
	}else{
		return false;
	}
}
int main(int argc, char** argv) {
	int year;
	int ans;
	ans=0;
	cin>>year;
	int rnmonth[13]={0,31,29,31,30,31,30,31,31,30,31,30,31};
	int pnmonth[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
	int month[13];
	// 星期四,星期五,星期六,星期日,星期一,星期二,星期三 
	int sumdays=0;
	for(int i=1998;i<year;i++){
		if(isrn(i)){
			sumdays+=366;
		}else{
			sumdays+=365;
		}
	}
	bool yearflag;
	yearflag = isrn(year);
	if(yearflag){
		memcpy(month,rnmonth,sizeof(rnmonth));
	}else{
		memcpy(month,pnmonth,sizeof(pnmonth));
	}
	for(int i=1;i<12;i++){
		sumdays+=month[i-1];
		if((sumdays+13-1)%7==1){
			cout<<i<<endl; 
			ans+=1;
		}	
	}
	cout<<ans;
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值