본문 바로가기

코딩 테스트/프로그래머스

[Level 1, C++] 2016년

728x90
반응형

 

시간을 계산해주는 방법을 찾아서 2016년 1월 1일 부터 해당 날짜까지 시간을 계산해서 요일을 구한다.

 


#include <string>
#include <vector>
#include <ctime>
#include <iostream>
using namespace std;

string solution(int a, int b) {
    string answer = "";
    
    time_t start, end;
    struct tm s_time;
    int tm_day, tm_hour, tm_min, tm_sec;
    double diff;
    
    string day[7] = { "SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT" };
    
    s_time.tm_year = 2016 - 1900;
    s_time.tm_mon = 1 - 1;
    s_time.tm_mday = 1;
    s_time.tm_hour = 0, s_time.tm_min = 0, s_time.tm_sec = 0; s_time.tm_isdst = 0;
    
    start = mktime(&s_time);
    
    s_time.tm_year = 2016 - 1900;
    s_time.tm_mon = a - 1;
    s_time.tm_mday = b;
    
    end = mktime(&s_time);
    
    diff = difftime(end, start);
    tm_day = diff / (60 * 60 * 24);
    
    answer = day[(tm_day + 5) % 7];
    
    return answer;
}

 

728x90
반응형