๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค/์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ’€์ด(Java)

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค_2016๋…„

by carrot0911 2024. 12. 13.

๋ฌธ์ œ ์„ค๋ช…

https://school.programmers.co.kr/learn/courses/30/lessons/12901

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

SW๊ฐœ๋ฐœ์ž๋ฅผ ์œ„ํ•œ ํ‰๊ฐ€, ๊ต์œก, ์ฑ„์šฉ๊นŒ์ง€ Total Solution์„ ์ œ๊ณตํ•˜๋Š” ๊ฐœ๋ฐœ์ž ์„ฑ์žฅ์„ ์œ„ํ•œ ๋ฒ ์ด์Šค์บ ํ”„

programmers.co.kr

2016๋…„ 1์›” 1์ผ์€ ๊ธˆ์š”์ผ์ž…๋‹ˆ๋‹ค. 2016๋…„ a์›” b์ผ์€ ๋ฌด์Šจ ์š”์ผ์ผ๊นŒ์š”? ๋‘ ์ˆ˜ a , b๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ 2016๋…„ a์›” b์ผ์ด ๋ฌด์Šจ ์š”์ผ์ธ์ง€ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜, solution์„ ์™„์„ฑํ•˜์„ธ์š”. ์š”์ผ์˜ ์ด๋ฆ„์€ ์ผ์š”์ผ๋ถ€ํ„ฐ ํ† ์š”์ผ๊นŒ์ง€ ๊ฐ๊ฐ
SUN,MON,TUE,WED,THU,FRI,SAT
์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด a=5, b=24๋ผ๋ฉด 5์›” 24์ผ์€ ํ™”์š”์ผ์ด๋ฏ€๋กœ ๋ฌธ์ž์—ด "TUE"๋ฅผ ๋ฐ˜ํ™˜ํ•˜์„ธ์š”.

์ œํ•œ ์‚ฌํ•ญ

  • 2016๋…„์€ ์œค๋…„์ž…๋‹ˆ๋‹ค.
  • 2016๋…„ a์›” b์ผ์€ ์‹ค์ œ๋กœ ์žˆ๋Š” ๋‚ ์ž…๋‹ˆ๋‹ค. (13์›” 26์ผ์ด๋‚˜ 2์›” 45์ผ ๊ฐ™์€ ๋‚ ์งœ๋Š” ์ฃผ์–ด์ง€์ง€ ์•Š์Šต๋‹ˆ๋‹ค.)

์ž…์ถœ๋ ฅ ์˜ˆ

a b result
5 24 "TUE"

 

๋‚ด๊ฐ€ ์ž‘์„ฑํ•œ ์ฝ”๋“œ

class Solution {
    public String solution(int a, int b) {
        String answer = "";
        
        int[] days = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        
        int totalDays = 0;
        for (int i = 1; i < a; i++) {
            totalDays += days[i];
        }
        totalDays += b;
        
        String[] week = {"FRI", "SAT", "SUN", "MON", "TUE", "WED", "THU"};
        answer = week[(totalDays-1) % 7];
        
        return answer;
    }
}

์ฝ”๋“œ ์„ค๋ช…

  • int[ ] days = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} : ๊ฐ ์›”์˜ ์ผ์ˆ˜๋ฅผ ์ €์žฅํ•œ days ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•œ๋‹ค. 1์›”๋ถ€ํ„ฐ 12์›”๊นŒ์ง€์˜ ์ผ์ˆ˜๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ์ €์žฅํ•˜๊ณ , ๋ฐฐ์—ด์˜ ์ฒซ ๋ฒˆ์งธ ๊ฐ’์€ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค.
  • int totalDays = 0 : ์ด์ผ์ˆ˜๋ฅผ ๋ˆ„์ ํ•˜์—ฌ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•œ ๋ณ€์ˆ˜ totalDays๋ฅผ ์ƒ์„ฑํ•˜๊ณ  0์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค.
  • for (int i = 1; i < a; i++) { totalDays += days[i] } : for๋ฌธ์„ ์‚ฌ์šฉํ•ด์„œ 1์›”๋ถ€ํ„ฐ a-1์›”๊นŒ์ง€์˜ ์ผ์ˆ˜๋ฅผ totalDays์— ๋”ํ•œ๋‹ค. days[i]๋Š” i์›”์˜ ์ผ์ˆ˜๋ฅผ ์˜๋ฏธํ•œ๋‹ค.
  • totalDays += b : totalDays ๋ณ€์ˆ˜์— b ๊ฐ’์„ ๋”ํ•œ๋‹ค. ์ž…๋ ฅ๋œ ๋‚ ์งœ๊นŒ์ง€์˜ ์ด์ผ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•œ๋‹ค.
  • String[ ] week = {"FRI", "SAT", "SUN", "MON", "TUE", "WED", "THU"} : ์š”์ผ์„ ์ €์žฅํ•œ week ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•œ๋‹ค. ๊ธˆ์š”์ผ์„ ๊ธฐ์ค€์œผ๋กœ ์š”์ผ ์ˆœ์„œ๋ฅผ ์ €์žฅํ•œ๋‹ค.
  • anwer = week[(totalDays - 1) % 7] (totalDays - 1) % 7์„ ๊ณ„์‚ฐํ•˜์—ฌ, week ๋ฐฐ์—ด์˜ ํ•ด๋‹น ์ธ๋ฑ์Šค ๊ฐ’์„ answer์— ์ €์žฅํ•œ๋‹ค. totalDays - 1์€ ๋‚ ์งœ ๊ณ„์‚ฐ์„ ๋ฐฐ์—ด ์ธ๋ฑ์Šค์— ๋งž์ถ”๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•œ๋‹ค.