반응형
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
풀이 >
입력된 월의 전월의 일수를 모두 더한 후 입력된 날짜를 더해 7로 나눈 나머지값으로 요일을 구함
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Baekjoon1924 {
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb=new StringBuilder();
String str=br.readLine();
String[] values=str.split(" ");
int month=Integer.parseInt(values[0]);
int days=Integer.parseInt(values[1]);
int totalMonth=0;
switch(month){
case 12:totalMonth+=30;
case 11:totalMonth+=31;
case 10:totalMonth+=30;
case 9:totalMonth+=31;
case 8:totalMonth+=31;
case 7:totalMonth+=30;
case 6:totalMonth+=31;
case 5:totalMonth+=30;
case 4:totalMonth+=31;
case 3:totalMonth+=28;
case 2:totalMonth+=31;
case 1:break;
}
switch((totalMonth+days)%7) {
case 1:sb.append("MON");break;
case 2:sb.append("TUE");break;
case 3:sb.append("WED");break;
case 4:sb.append("THU");break;
case 5:sb.append("FRI");break;
case 6:sb.append("SAT");break;
case 0:sb.append("SUN");break;
}
System.out.println(sb);
}
}
다른 풀이 >
배열을 사용하기!
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Baekjoon1924 {
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
String str = br.readLine();
String[] values = str.split(" ");
int month = Integer.parseInt(values[0]);
int days = Integer.parseInt(values[1]);
int totalMonth = 0;
int[] months = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
String[] day = { "SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT" };
for (int i = 0; i < month; i++) {
totalMonth += months[i];
}
totalMonth += days;
sb.append(day[totalMonth % 7]);
System.out.println(sb);
}
}
두 풀이는 메모리와 시간의 차이가 거의없었따
반응형
'알고리즘 > 문제풀이' 카테고리의 다른 글
[JAVA]백준 - 1271.엄청난 부자2 (0) | 2020.11.04 |
---|---|
[JAVA]백준 - 2920.음계 (0) | 2020.11.04 |
[JAVA]백준 - 2588.곱셈 (0) | 2020.10.25 |
[JAVA]백준 - 1541.잃어버린 괄호 (0) | 2020.09.14 |
[JAVA]백준 - 10162.전자레인지 (0) | 2020.09.12 |