Problem 19

C

 1#include<stdio.h>
 2const int NUM_MONTHS_IN_99_YEARS = 1200;
 3struct month{
 4    int num_days;
 5};
 6
 7int main(){
 8    int upper_bound = 36135; //number of days in 99 years  
 9    struct month iterator[NUM_MONTHS_IN_99_YEARS]; //number of months in 99 years
10    int month_array = 0;
11    for (int i = 1901; i < 2001; i++){
12        iterator[month_array].num_days = 31;
13        month_array++;
14        if(i % 4 == 0){
15            iterator[month_array].num_days = 29;
16        }   
17        else{
18            iterator[month_array].num_days = 28;
19        }
20        month_array++;
21        iterator[month_array].num_days = 31;
22        month_array++;
23        iterator[month_array].num_days = 30;
24        month_array++;
25        iterator[month_array].num_days = 31;
26        month_array++;
27        iterator[month_array].num_days = 30;
28        month_array++;
29        iterator[month_array].num_days = 31;
30        month_array++;
31        iterator[month_array].num_days = 31;
32        month_array++;
33        iterator[month_array].num_days = 30;
34        month_array++;
35        iterator[month_array].num_days = 31;
36        month_array++;
37        iterator[month_array].num_days = 30;
38        month_array++;
39        iterator[month_array].num_days = 31;
40        month_array++;
41    }
42    int num_sun_first = 0;
43    int cur_sunday = 6; //first sunday was the fifth of january in 1901
44    int i = 0;
45    while (i <= NUM_MONTHS_IN_99_YEARS){
46        cur_sunday += 7;
47        if(cur_sunday > iterator[i].num_days){
48            if(cur_sunday - iterator[i].num_days == 1){
49                num_sun_first++;
50            }
51            cur_sunday = cur_sunday - iterator[i].num_days;
52            i++;
53        }
54
55    }
56    printf("%d\n", num_sun_first);
57    return 0;
58}