Algorithm training black Friday time limit: 1.0s memory limit: 512.0MB
Problem description
Some Westerners are more superstitious, if the number 13th in a month is exactly Friday, they will feel unlucky, with the ancients, that is, "things are not appropriate." Ask you to write a program that counts how many times in a particular year it's 13th and Friday to help your superstitious friends solve problems.
Description: (1) There are 365 days a year, leap years have 366 days, the so-called leap year, which can be divisible by 4 and not divisible by 100 years, or can be divisible by 100 can also be divisible by 400 years; (2) It is known that January 1, 1998 is Thursday, and the year that the user enters must be greater than or equal to 1998 years
Input format: Enter only one line, that is, a specific year (greater than or equal to 1998).
Output format: The output is only one line, that is, in this year, how many times there are 13th and Friday is the case.
Input and Output Sample sample input 1998 sample output
3
Problem Analysis: First we can exit 1998 January 13 is Tuesday, then on this bit basis, assuming a day away from this day difference n days, then this day is the week of the algorithm is: (2+n)%7, if equal to zero, that is Sunday, otherwise calculated is how much is the week, In order to simplify, first calculate the number of days in terms of the difference of the year, and then one for the month in the unit
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int date[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int year,month,day;
int n;
int Count;
int main ()
{
int cnt;
int daycount;
int cal;
CIN >> N;
Daycount = CNT = 0;
Cal = 2;
Count = 0;
for (int i=1998; i<n; i++)
{
//To determine leap year
if ((i% 4 ==0 && i% 100! = 0) | | (i% = = 0 && I% = = 0))
CNT + = 366;
else
cnt + = 365;
}
Cal = (cal + CNT)% 7;
if (cal = = 0)
cal = 7;
if (cal = = 5)
count++;
for (int i=1; i<=11; i++)
{
cnt = 0;
if (i = = 2)
{
if ((n% 4 ==0 && n% 100! = 0) | | (n% = = 0 && n = = 0))
CNT + = Date[i] + 1;
else
cnt + = Date[i];
}
else
cnt + = Date[i];
Cal = (cal + CNT)% 7;
if (cal = = 0)
cal = 7;
if (cal = = 5)
count++;
}
cout << Count << Endl;
return 0;
}