알고리즘

[백준] 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]);
	}
}