프로그래밍응용/오답노트

프로그래머스, 2016년 문제(C++, Python)

photoner 2021. 2. 25. 22:41
728x90
반응형
 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

모든 문제의 출처는 위임을 밝힘ㅋ

[ C++ ]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <string>
#include <vector>
 
using namespace std;
 
string solution(int a, int b) {
    string answer = "";
    
    int month[12= { 312931303130313130313031 };
    string day_string[7= { "FRI""SAT""SUN""MON""TUE""WED""THU" };
    int sumday = 0// 2016년 1월 1일부터 ~ 2016년 a월 b일까지 총 날짜 수를 구함.
    
    for(int i=1; i<a; i++)
        sumday += month[i-1]; // a월 직전 달까지 모든 날짜를 더한 후
    sumday += (b-1); // a월의 b일까지 더하고 1일부터 시작하니 1일을 뺀다.
    
    return day_string[sumday%7];
}
cs

[ Python ]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
def solution(a, b):
    answer = ''
    month = [ 312931303130313130313031 ]
    day_string = [ "SUN""MON""TUE""WED""THU""FRI""SAT" ]
    sumday = 0
    
    for i in range(1, a):
        sumday += month[i-1]
    sumday = sumday + (b-1)
    
    return day_string[(sumday+5)%7]
    
    # 아래 한 줄로 7번 라인 ~ 11번 라인을 대체할 수 있다.
    # return day_string[(sum(month[:a-1-1])+b-1+5)%7] 
    # 위의 내용들과 같다.
cs
728x90
반응형