[WEEKLYCALENDAR] Weekly Calendar
개발이야기/알고스팟2022. 1. 3. 14:14
Java 로 풀어보는 알고리즘입니다. 📖
코딩테스트를 대비하여 JAVA1.8 부터 제공되는 함수형 API 는 사용하지 않았습니다.
문제 : https://www.algospot.com/judge/problem/read/WEEKLYCALENDAR
풀이입니다. 🤔
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
import java.util.*;
public class Main {
private static final String[] weekStrings = new String[]{
"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
};
private static final int[] endDayOfMonths = new int[]{
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int cases = scanner.nextInt();
while (cases-- > 0) {
List<Integer> daysOfWeek = getDaysOfWeek(scanner.nextInt(), scanner.nextInt(), scanner.next());
System.out.printf(
"%d %d %d %d %d %d %d\n",
daysOfWeek.get(0),
daysOfWeek.get(1),
daysOfWeek.get(2),
daysOfWeek.get(3),
daysOfWeek.get(4),
daysOfWeek.get(5),
daysOfWeek.get(6)
);
}
}
private static List<Integer> getDaysOfWeek(int m, int d, String s) {
int pivot = getWeekIndex(s);
List<Integer> ret = new ArrayList<>();
for (int i = 0; i <= pivot; ++i) {
int candidate = d - i;
ret.add(0, (candidate > 0) ? candidate : endDayOfMonths[(m - 2 < 0) ? 11 : m - 2] + candidate);
}
for (int i = 1, len = 7 - pivot; i < len; ++i) {
int candidate = d + i;
ret.add((candidate <= endDayOfMonths[m - 1]) ? candidate : candidate - endDayOfMonths[m - 1]);
}
return ret;
}
private static int getWeekIndex(String s) {
for (int i = 0, len = weekStrings.length; i < len; ++i) if (weekStrings[i].equals(s)) return i;
throw new IllegalArgumentException();
}
}
|
cs |
이 포스트를 읽어주셔서 감사합니다. 🙇🏻♂️
반응형
'개발이야기 > 알고스팟' 카테고리의 다른 글
[BRACKETS2] Mismatched Brackets (0) | 2022.01.03 |
---|---|
[SHISENSHO] Shisen-sho (0) | 2022.01.03 |
[CLOCKSYNC] Synchronizing Clocks (0) | 2022.01.02 |
[HAMMINGCODE] Hamming Code (0) | 2021.12.30 |
[WEIRD] Weird Numbers (0) | 2021.12.30 |