19 - counting sundays

ou are given the following information, but you may prefer to do some research for yourself.

  • 1 Jan 1900 was a Monday.
  • Thirty days has September,
    • April, June and November.
    • All the rest have thirty-one,
    • Saving February alone,
    • Which has twenty-eight, rain or shine.
    • And on leap years, twenty-nine.
  • A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400.

How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?

cevap : 171

link

Derleme ortami Visual studio community c++

// project euler problem 19
// yasin tasan --> nov 2016

#include "stdafx.h"
#include "conio.h"

int main()
{
    unsigned int i = 0; 
    unsigned int mount_counter = 1;
    unsigned int year_counter = 1900;
    unsigned int sunday_counter = 0;

    while (year_counter < 2001)
    {
        printf("----------------------------------------------------------------------");
        printf("\n");
        printf("%d yili analizi:\n", year_counter);

        while (mount_counter < 13)
        {
            printf("%d yili %d. ay pazar gunleri:\n", year_counter, mount_counter);

            if (i != 0)
            {
                printf("%d\n", i);
                if (i==1 && year_counter >=1901)
                {
                    sunday_counter++;
                }
            }

            while (i<31)
            {
                i = i + 7;
                if (i<=31)
                {
                    printf("%d\n", i);
                    if (i == 1 && year_counter >= 1901)
                    {
                        sunday_counter++;
                    }
                }
                if (((mount_counter == 1) || (mount_counter == 3) || (mount_counter == 5) || (mount_counter == 7) || (mount_counter == 8) || (mount_counter == 10) || (mount_counter == 12)) && i>=31) // 31 ceken aylar
                {
                    i = i % 31;
                    break;
                }
                if (((mount_counter == 4) || (mount_counter == 6) || (mount_counter == 9) || (mount_counter == 11)) && i>=30) // 30 ceken aylar
                {
                    i = i % 30;
                    break;
                }
                if ( (mount_counter == 2) && (i >= 29) && (year_counter%4==0) && (year_counter != 1900) ) // artik yillardaki subat
                {
                    i = i % 29;
                    break;
                }
                else if ( (mount_counter == 2) && i>28 ) // subat
                {
                    i = i % 28;
                    break;
                }
            }
            mount_counter++;
        } // while while (mount_counter < 13)
        mount_counter = 1;
        year_counter++;
    }

    printf("ayin 1'ine denk gelen pazar gunleri sayisi: %d", sunday_counter);

    _getch();
    return 0;
}