본문 바로가기

프로그래머스

프로그래머스 - 2016년

내 코드

class Solution {
public String solution(int a, int b) {
String answer = "";

        
String[] dayStr = {"FRI", "SAT", "SUN", "MON", "TUE", "WEN", "THU"};

int[] days = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int totalDays = 0;

for (int i = 0; i < a - 1; i++) {
totalDays += days[i];
}

totalDays += b;

int dayStrIdx = (totalDays - 1) % dayStr.length;
answer = dayStr[dayStrIdx];
return answer;
}
}



다른 사람 코드

class Solution {
public String solution(int a, int b) {
String answer = "";

String w[] = {"FRI", "SAT", "SUN", "MON", "TUE", "WED", "THU"};
int m[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int total = 0;

for (int i = 0; i < a - 1; i++) {
total += m[i];
}

total += b;
answer = w[(total - 1) % 7];
return answer;
}
}



도대체 뭐가 다른 건지 모르겠는데

내 코드는 안되고, 아래 코드는 된다. 


테스트 케이스 2개정도 오류가 발생한다.


혹시 아시는 분 알려주시면 감사하겠습니다.