Q 1609 [Algorithm Improvement VIP]Black Friday (C Language)

题目 1609 [算法提高VIP]黑色星期五 (C语言)

Q 1609: [Algorithm Improvement VIP]Black Friday

Time limit: 1Sec Memory Limit: 128MB

Title Description

Some Westerners are superstitious, and if the 13th of a month falls on a Friday, they feel that it is not auspicious, or in ancient times, “bad for everything”. Write a program to find out how many times in a given year the 13th falls on a Friday, to help your superstitious friend solve his problem. Note: (1) There are 365 days in a year, and 366 days in a leap year. A leap year is a year that is divisible by 4 and not by 100, or a year that is divisible by both 100 and 400; (2) January 1, 1998 is known to be a Thursday, and the year entered by the user must be greater than or equal to 1998.

Input

The input has only one line, i.e. a specific year (greater than or equal to 1998).

Output

The output has only one line, i.e. how many times in the year it occurred that it was both the 13th and a Friday.

Sample Input

1
1998

Sample Output

1
3

C Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include<stdio.h>
int day[14];
int month[2][20]={{31,28,31,30,31,30,31,31,30,31,30,31},{31,29,31,30,31,30,31,31,30,31,30,31}};
int leap(int year)
{
    if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
    {
        //It's a leap year
        return 1;
    }else{
        //Not a leap year
        return 0;
    }
}
void f(int n)
{
    int i,j;
    long sum=0;
    int count = 0;
    for(i=1998;i<n;i++)
    {
        if(leap(i))
        {
            sum += 366;
        }else{
            sum += 365;
        }
    }
    //Count how many days from January 1, 1998 to the 13th of each month
    if(leap(n))
    {
        for(i=1;i<13;i++)
        {
            day[i]=12+sum;
            for(j=0;j<i-1;j++)
            {
                day[i] +=month[1][j];
            }
        }
    }else{
        for(i=1;i<13;i++)
        {
            day[i]=12+sum;
            for(j=0;j<i-1;j++)
            {
                day[i] += month[0][j];
            }
        }
    }
    //Count the number of occurrences of week 5
    for(i=1;i<=12;i++)
    {
        if((day[i]-3)%7==5)
        {
            count ++;
        }  
    }
    printf("%d\n",count);
}
int main()
{
    int year;
    scanf("%d",&year);
    f(year);
    return 0;
}

All through C语言网 compile and run.

Built with Hugo
Theme Stack designed by Jimmy