Given a date, return the corresponding day of the week for that date.
The input is given as three integers representing the day
, month
and year
respectively.
Return the answer as one of the following values {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}
.
cpp
로 문제를 좀 풀어보고 있어서 cpp
의 ctime
을 활용했다.#include <ctime>
내의 tm
구조체를 선언하고mktime()
을 통해 구조체로 특정 날짜 값의 요일값을 계산한다.tm_wday
로 리턴된 요일값으로 결과 리턴#include <bits/stdc++.h>
class Solution {
public:
string dayOfTheWeek(int day, int month, int year) {
string days[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
tm time_in = {};
time_in.tm_year = year - 1900;
time_in.tm_mon = month - 1;
time_in.tm_mday = day;
mktime(&time_in);
return days[time_in.tm_wday];
}
};