Notice
Recent Posts
Recent Comments
Link
«   2025/07   »
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
Archives
Today
Total
관리 메뉴

Mintaka's log

[백준] 1924번 본문

알고리즘

[백준] 1924번

_해랑 2022. 5. 17. 22:30

https://www.acmicpc.net/problem/1924

 

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{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int[] monthDay = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
		StringTokenizer st = new StringTokenizer(br.readLine(), " ");
		int total = 0;
		int month = Integer.parseInt(st.nextToken());
		
		for(int i = 1; i < month; i++) {
			total += monthDay[i];
		}
		total += Integer.parseInt(st.nextToken());
		
		if(total%7 == 1) {
			System.out.println("MON");
		}else if (total%7==2){
			System.out.println("TUE");
		}else if (total%7==3){
			System.out.println("WED");
		}else if (total%7==4){
			System.out.println("THU");
		}else if (total%7==5){
			System.out.println("FRI");
		}else if(total%7==6){
			System.out.println("SAT");
		}else if (total%7==0){
			System.out.println("SUN");
		}
	}
}

 

근데 굳이 if문을 저렇게 길게 써줄 필요도 없었다. 결과를 배열에 저장하고 바로 출력으로 가능. 

 

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));
		int[] monthDay = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
		String[] date = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"}; 
		StringTokenizer st = new StringTokenizer(br.readLine(), " ");
		int total = 0;
		int month = Integer.parseInt(st.nextToken());
		
		for(int i = 1; i < month; i++) {
			total += monthDay[i];
		}
		total += Integer.parseInt(st.nextToken());
		
		System.out.println(date[total%7]);
	}
}

'알고리즘' 카테고리의 다른 글

[백준] 10991- 아니 왜 안됨?-해결!  (0) 2022.05.24
[백준] 10818  (0) 2022.05.17
[JAVA]Scanner 사용 중 nextInt 다음 nextLine 사용시 에러  (0) 2022.05.12
[백준]10951번  (0) 2022.05.09
[백준]10950번  (0) 2022.05.09