본문 바로가기
알고리즘/Samsung

SW 1948. 날짜 계산기

by son_i 2023. 5. 2.
728x90

SW Expert Academy

 

SW Expert Academy

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

swexpertacademy.com

자바로 품

너무 간단하게 해결했다.

각 월의 일 수를 배열에 담아놓고 시작

월이 같다면 끝 일 - 처음 일 + 1 한 거 result에 담고 출력

월이 다르다면 시작월 ~ 끝 월 반복문 돌려서 배열 값 result에 +=해서 담고

  끝일 - 처음일 +1 한 것도 += 해서 넣고 출력

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;

/*
   사용하는 클래스명이 Solution 이어야 하므로, 가급적 Solution.java 를 사용할 것을 권장합니다.
   이러한 상황에서도 동일하게 java Solution 명령으로 프로그램을 수행해볼 수 있습니다.
 */
public class Solution
{
	public static void main(String args[]) throws Exception
	{
		//System.setIn(new FileInputStream("C:\\so_Project\\workspace\\BaekJoon\\src\\algorithm\\input.txt"));

		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
		int T = Integer.parseInt(br.readLine()); //테스트 케이스
	
		int month[] = {31,28,31,30,31,30,31,31,30,31,30,31};
	
		for(int i=1;i<=T;i++) { //하나의 테스트케이스마다 반복
			
			int result =0;
			StringTokenizer st = new StringTokenizer(br.readLine());
			int fmonth = Integer.parseInt(st.nextToken());
			int fday = Integer.parseInt(st.nextToken());
			int smonth = Integer.parseInt(st.nextToken());
			int sday = Integer.parseInt(st.nextToken());
			
			if(fmonth == smonth) {
				result = sday-fday+1;
			}
			else {
				for(int j=fmonth;j<smonth;j++) 
					result += month[j-1];
				result += sday-fday+1;
			}
			bw.write("#"+i+" "+result+"\n");
		}//하나의 테스트 케이스마다 반복문 끝
		bw.flush();
		bw.close();
	}
}

 

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

SW 1945. 간단한 소인수분해  (0) 2023.05.02
SW 1946. 간단한 압축 풀기  (0) 2023.05.02
SW 1961. 숫자 배열 회전  (0) 2023.04.27
SW 1983.조교의 성적 매기기  (1) 2023.04.18
SW 2001. 파리퇴치  (0) 2023.04.18