티스토리 뷰

PS/programmers

Level1) 2016년

kingsubin 2020. 9. 8. 14:19
class Solution {
    public static String solution(int a, int b) {
        int[] days = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
        String[] day_week = { "THU", "FRI", "SAT", "SUN", "MON", "TUE", "WED" };
        int totalDays = 0;

        if (a == 1) {
            totalDays = b;
        } else {
            for (int i = 0; i < a-1; i++) {
                totalDays += days[i];
            }
            totalDays += b;
        }
        return day_week[totalDays%7];
    }

}

- 적다보니까 규칙이 보여서 월마다의 일수배열, 요일배열 만들어서 풀이

- 총 일수를 구하고 요일 배열에서 %7 을한 값

 

 

class TryHelloWorld
{
    public String getDayName(int month, int day)
    {
        Calendar cal = new Calendar.Builder().setCalendarType("iso8601")
                        .setDate(2016, month - 1, day).build();
        return cal.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.SHORT, new Locale("ko-KR")).toUpperCase();
    }
    public static void main(String[] args)
    {
        TryHelloWorld test = new TryHelloWorld();
        int a=5, b=24;
        System.out.println(test.getDayName(a,b));
    }
}

- 다른 사람 풀이 중 이런게 있었는데 봐도 나중에 쓰기 힘들 것 같다

 

 


programmers.co.kr/learn/challenges

'PS > programmers' 카테고리의 다른 글

Level1) 같은 숫자는 싫어  (0) 2020.09.08
Level1) 가운데 글자 가져오기  (0) 2020.09.08
Level1) K번째 수  (0) 2020.09.08
Level1) 모의고사  (0) 2020.09.08
Level1) 체육복  (0) 2020.09.08