문제
https://programmers.co.kr/learn/courses/30/lessons/12901
코딩테스트 연습 - 2016년
2016년 1월 1일은 금요일입니다. 2016년 a월 b일은 무슨 요일일까요? 두 수 a ,b를 입력받아 2016년 a월 b일이 무슨 요일인지 리턴하는 함수, solution을 완성하세요. 요일의 이름은 일요일부터 토요일까
programmers.co.kr
코드
// 나의풀이
class Solution {
public String solution(int month, int day) {
String result = "";
// 월별 일수(1 ~ 12월)
int[] month_days = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
String[] week = { "FRI", "SAT", "SUN", "MON", "TUE", "WED", "THU" };
int all_days = 0;
for (int i = 0; i < month - 1; i++) {
all_days += month_days[i];
}
System.out.println(all_days);
all_days = all_days + day - 1;
System.out.println(all_days);
result = week[all_days % 7];
return result;
}
}
// 다른사람의 풀이
class Solution {
public String solution(int a, int b) {
String answer = "";
Calendar cal = Calendar.getInstance();
cal.set(2016, a-1, b);
Date date = cal.getTime();
SimpleDateFormat sdf = new SimpleDateFormat("E", Locale.ENGLISH);
answer = sdf.format(date).toUpperCase();
return answer;
}
}
'프로그래머스 > LEVEL 1' 카테고리의 다른 글
[프로그래머스/LEVEL1] 문자열 다루기 기본 (0) | 2022.01.15 |
---|---|
[프로그래머스/LEVEL1] 내적 (0) | 2022.01.15 |
[프로그래머스/LEVEL1] 수박수박수박수박수박수? (0) | 2022.01.15 |
[프로그래머스/LEVEL1] 문자열 내 p와 y의 개수 (0) | 2022.01.15 |
[프로그래머스/LEVEL1] 나누어 떨어지는 숫자 배열 (0) | 2022.01.15 |