cíu

mail@pastecode.io avatar
unknown
c_cpp
23 days ago
2.2 kB
2
Indexable
Never
class Solution {
public:
    string dayOfTheWeek(int day, int month, int year) {
        int date[60000];

        //ngày 1 tháng 1 năm 1971 là thứ 2

        int mon = 1, tue = 2, wed = 3, thu = 4, fri = 5, sat = 6, sun = 7;

        int cnt = 6745;
        while (cnt) {
            date[mon] = 2; mon += 7;
            date[tue] = 3; tue += 7;
            date[wed] = 4; wed += 7;
            date[thu] = 5; thu += 7;
            date[fri] = 6; fri += 7;
            date[sat] = 7; sat += 7;
            date[sun] = 8; sun += 7;

            cnt--;
        }

        int sum_of_date = 0;

        for (int i = 1971; i < year; i++) {
            sum_of_date += 365;

            if (year % 4 == 0) {
                if (year % 100 == 0) {
                    if (year % 400 == 0) 
                        sum_of_date++;
                }
                else sum_of_date++;
            }
        }

        for (int i = 1; i < month; i++) {
            if (i == 1 || i == 3 || i == 5 || i == 7 
                || i == 8 || i == 10 || i == 12)
                sum_of_date += 31;
            else if (i == 4 || i == 6 || i == 9 || i == 11)
                sum_of_date += 30;
            else if (i == 2)
                sum_of_date += 28;

            if (i == 2 && year % 4 == 0) {
                if (year % 100 == 0) {
                    if (year % 400 == 0) 
                        sum_of_date++;
                }
                else sum_of_date++;
            }
        }

        sum_of_date += day;

        string the_day;

        if (date[sum_of_date] == 2) 
            the_day = "Monday";
        else if (date[sum_of_date] == 3)
            the_day = "Tuesday";
        else if (date[sum_of_date] == 4) 
            the_day = "Wednesday";
        else if (date[sum_of_date] == 5)
            the_day = "Thursday";
        else if (date[sum_of_date] == 6) 
            the_day = "Friday";
        else if (date[sum_of_date] == 7)
            the_day = "Saturday";
        else if (date[sum_of_date] == 8) 
            the_day = "Sunday";

        return the_day;
    }
};
Leave a Comment