본문 바로가기

Java/SWEA

[JAVA] SWEA 1948. 날짜 계산기

https://swexpertacademy.com/main/code/problem/problemDetail.do?problemLevel=2&contestProbId=AV5PnnU6AOsDFAUq&categoryId=AV5PnnU6AOsDFAUq&categoryType=CODE&problemTitle=&orderBy=FIRST_REG_DATETIME&selectCodeLang=JAVA&select-1=2&pageSize=10&pageIndex=2

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

 

 

정답 

 

import java.util.*;

class Solution {
	static int days[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
	
	//날짜 누적합 
	static int Accdays(int month) {
		int acc=0;
		for(int i=0; i<=month; i++)
		{
			acc+=days[i];
		}		
		return acc;
	}
	
	
    public static void main(String args[]) throws Exception {
        
    	Scanner sc = new Scanner(System.in);
        int T = sc.nextInt(); 
        
        for (int t = 1; t <= T; t++)
        { 
        	int m1 = sc.nextInt();
        	int d1 = sc.nextInt();
        	int m2 = sc.nextInt();
        	int d2 = sc.nextInt();
        	int answer = 0;
        	
        	answer = (Accdays(m2-1)+d2)-(Accdays(m1-1)+d1)+1;
        	
        	System.out.printf("#%d %d\n",t,answer);
        	
        }
    }
}