Blue Bridge Cup ALGO-105 algorithm to train Black Friday

Black Friday algorithm training  

Time limit: 1.0s Memory Limit: 512.0MB

 

Problem Description
  Some Westerners compare superstition, if the number 13 is just a month Friday, they will feel less lucky, with the ancients of the view that "not everything go." You write a program, the statistics in a given year, there have been many times both No. 13 is the case Friday, a friend to help you solve the problem of superstition.
  Description: 365 days (1) year leap year 366 days, the so-called leap year, the year that is divisible by 4 can not be divisible by 100, or both can be divisible by 100 years divisible 400; (2) known January 1, 1998 is Thursday, the year entered by the user is certainly greater than or equal to 1998.
  Input formats: Enter only one line, that is a particular year (greater than or equal to 1998).
  Output formats: Output only one line, that is, during the year, there have been many times both No. 13 is the case Friday.
Sample input and output

Sample input

1998

Sample Output

3

 

#include <stdio.h>

int days_of_month[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int is_leap(int year)
{
    if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
        return 1;
    else
        return 0;
}

int day_of_week(int year, int month, int day)
{
    int days_to_19980101 = 0;

    for (int y = 1998; y < year; ++y)
    {
        if (is_leap(y))
            days_to_19980101 += 366;
        else
            days_to_19980101 += 365;
    }
    for (int m = 1; m < month; ++m)
    {
        days_to_19980101 += days_of_month[m];
        if (m == 2 && is_leap(year))
            days_to_19980101 += 1;
    }
    days_to_19980101 += day - 1;

    return (days_to_19980101 % 7 + 4) % 7;
}

int main()
{
    int year;
    int cnt = 0;

    scanf("%d", &year);
    for (int month = 1; month <= 12; ++month)
    {
        if (day_of_week(year, month, 13) == 5)
            cnt++;
    }
    printf("%d", cnt);

    return 0;
}

 

Published 221 original articles · won praise 40 · views 40000 +

Guess you like

Origin blog.csdn.net/liulizhi1996/article/details/104023924