프로그래머스 - 2016년[java]

스브코·2021년 11월 8일

문제 출처: https://programmers.co.kr/learn/courses/30/lessons/12901

문제 설명

month 와 day 를 변수로 보내주면 2016년에 저 날이 무슨요일인지 return 하면 된다.

문제 풀이 1

import java.util.*;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;

class Solution {
    public String solution(int a, int b) {
        LocalDate date = LocalDate.of(2016, a, b);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("E", Locale.ENGLISH);
        String dateformat = formatter.format(date).toUpperCase();
        return dateformat;
    }
}

항해99 자바 문법 뽀개기에서 가르쳐준 datetimeformatter를 사용해서 풀었다.

문제풀이 2

class Solution {
    public String solution(int a, int b) {
        String answer = "";
        int numOfDays = 0;
        if(a == 1) {
            numOfDays = b % 7;
        } else {
            int temp = (a - 1) * 30 + b;
            if(a == 2 || a == 4 || a == 5) {
                numOfDays = (temp + 1) % 7;
            } else if(a == 9 || a == 10) {
                numOfDays = (temp + 4) % 7;
            } else if (a == 6 || a == 7) {
                numOfDays = (temp + 2) % 7;
            } else if (a == 8) {
                numOfDays = (temp + 3) % 7;
            } else if (a == 11 || a == 12) {
                numOfDays = (temp + 5) % 7;
            } else {
                numOfDays = temp % 7;
            }
        }
        if(numOfDays == 0) {
            return "THU";
        } else if ( numOfDays == 1) {
            return "FRI";
        }else if ( numOfDays == 2) {
            return "SAT";
        }else if ( numOfDays == 3) {
            return "SUN";
        }else if ( numOfDays == 4) {
            return "MON";
        }else if ( numOfDays == 5) {
            return "TUE";
        }else {
            return "WED";
        }
    }
}

import를 아무것도 안하고 풀었다.... API 기억안나고 못보면 저렇게라도 해야...

문제 풀이 3

import java.time.*;
class Solution {
  public String solution(int a, int b) {
      return LocalDate.of(2016, a, b).getDayOfWeek().toString().substring(0,3);
  }
}

제일 깔끔하게 푼 솔루션.. 역시 함수를 많이 알아야...

profile
익히는 속도가 까먹는 속도를 추월하는 그날까지...

0개의 댓글