Blue Bridge: Black Friday

Blacks appear Friday

Problem Description

Some ⻄ ⽅ Face ⽐ than superstition, if a Month 13 Hao happens to be Friday, they will feel less lucky, Face Using ancient argument, it is

"Off-day." Please write ⼀ programs, statistics in a given year, there have been many times both 13 Hao separate warranty is the case Friday,
To help you solve the problem of superstition friends.
DESCRIPTION :( 1 ) ⼀ of 365 days, leap years 366 days, the so-called leap year, i.e. be 4 divisible and can not be 100 years divisible,
                   or 100 are both divisible also be 400 years divisible;
          ( 2 ) known in 1998 years 1 Month 1 date is Thursday, the user input START year Use certainly equal to or below approximately 1998 years.
Lose START format:
    START lose only ⼀ ⾏ that a specific year (equal to or below approximately 1998 years).
Output formats:
    Output only ⼀ ⾏ that in this ⼀ years, there have been many times both 13 No. separate warranty is the case Friday.
START input sample output
Sample lose START
1998
Sample Output
3
analysis:
1. According to Kim Larson enumerated formula
2.W =(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400) % 7
3. Note: There is a different from the other formulas in places where in the formula of: ⼀ ⼆ Month Month and year to the next higher as a ⼗ ⼗ four and three Month Month Example: The
If it is converted into 2018-1-1: 2017-13-1 ~ to represent START formula
#include <iostream>
using namespace std;
int ans = 0;
void f(int y, int m, int d) {
 if ( m == 1 || m == 2) {
      y--;
      m += 12;
 }
 if ((d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400) % 7 == 4 && d == 13) 
      ans++; 
}
int main() {
 int n;
 int m31[] = {1, 3, 5, 7, 8, 10, 12};
 int m30[] = {4, 6, 9, 11};
 int m2;
 cin >> n;
 if ( n % 4 == 0 && n % 100 != 0 || n % 400 == 0) {
      m2 = 29;
 } 
 else {
      m2 = 28;
 }
 for ( int i = 0; i < 7; i++)
    for ( int j = 1; j <= 31; j++)
          f(n, m31[i], j);
 for (int i = 0; i < 4; i++)
    for (int j = 1; j <= 30; j++)
         f(n, m30[i], j);
 for (int j = 1; j <= m2; j++)
      f(n, 2, j);
 cout << ans << endl;
 return 0; 
}

 

Published 736 original articles · won praise 123 · views 80000 +

Guess you like

Origin blog.csdn.net/S_999999/article/details/103355569