1924번: 2007년
첫째 줄에 빈 칸을 사이에 두고 x(1 ≤ x ≤ 12)와 y(1 ≤ y ≤ 31)이 주어진다. 참고로 2007년에는 1, 3, 5, 7, 8, 10, 12월은 31일까지, 4, 6, 9, 11월은 30일까지, 2월은 28일까지 있다.
www.acmicpc.net
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException{
int[] monthArr = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String date = br.readLine();
StringTokenizer st = new StringTokenizer(date);
int month = Integer.parseInt(st.nextToken());
int day = Integer.parseInt(st.nextToken());
int result = monthArr[month-1] + day;
if (result % 7 == 1) {
System.out.println("MON");
} else if (result % 7 == 2) {
System.out.println("TUE");
}else if (result % 7 == 3) {
System.out.println("WED");
}else if (result % 7 == 4) {
System.out.println("THU");
}else if (result % 7 == 5) {
System.out.println("FRI");
}else if (result % 7 == 6) {
System.out.println("SAT");
}else if (result % 7 == 0) {
System.out.println("SUN");
}
}
}
깨달은 점:
하드코딩 하지 않고 월마다 일 수를 배열에 담고 입력받은 월 수를 넣어서 반복문 돌리면 굳이 하드코딩할 필요가 없음...
요일도 배열에 담아 한번에 출력할 수 있음.. 하드코딩 하지 않아도 됨..... 조금 더 생각하고 구현에 익숙해지자.......
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
int[] month = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
String[] days = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};
int sum = 0;
for (int i = 0; i < x - 1; i++) {
sum += month[i];
}
sum += y;
System.out.println(days[sum % 7]);
}
}
'Java > AlgorithmPS' 카테고리의 다른 글
백준) 세로읽기(10798번) (0) | 2023.09.20 |
---|---|
백준) 팰린드롬수(1259번) (0) | 2023.09.20 |
백준) 부녀회장이 될테야(2775번) (0) | 2023.09.18 |
백준) 단어 공부(1157번) (0) | 2023.09.17 |
백준) 더하기 사이클(1110번) (0) | 2023.09.16 |