问题描述
有些西方人比较迷信,如果某个月的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;
}