-
[SWEA] [JAVA] [Difficulty 2] [1948] 날짜 계산기SW Expert Academy 2022. 11. 16. 21:36
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.StringTokenizer; public class Solution { public static void main(String[] args) throws Exception{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int T = Integer.parseInt(br.readLine()); // 테스트 케이스 ArrayList<Integer> arr = new ArrayList<>(); // 정답을 저장할 list int ans = 0; // 인덱스를 맞추기위해 0은 0월 , 1~12월의 마지막날짜 int cal[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; // 테스트케이스 for(int tc=0; tc<T; tc++) { StringTokenizer st = new StringTokenizer(br.readLine()," "); int m1 = Integer.parseInt(st.nextToken()); // 첫 번째 일자의 월 int d1 = Integer.parseInt(st.nextToken()); // 첫 번째 일자의 일 int m2 = Integer.parseInt(st.nextToken()); // 두 번째 일자의 월 int d2 = Integer.parseInt(st.nextToken()); // 두 번째 일자의 일 // 같은 달일 때 if(m1==m2) ans = d2-d1 + 1; // 다른 달일 때 else { // 첫번째 일자의 말일에서 해당일을 빼고 마지막 일자의 일수를 더해줌 ans = ( cal[m1] - d1 ) + d2; // 첫번째 일자의 월과 두번째 일자의 월 사이의 일수를 더해줌 for(int i=m1+1; i<m2; i++) { ans += cal[i]; } // 위에서 뺄셈 했을 때 당일도 빼지기 떄문에 빠진날을 +1 해줌 ans += 1; } arr.add(ans); } for(int i=1; i<=T; i++) { System.out.println("#" + i + " " + arr.get(i-1)); } } }
'SW Expert Academy' 카테고리의 다른 글
[SWEA] [JAVA] [Difficulty 2] [1966] 숫자를 정렬하자 (0) 2022.11.17 [SWEA] [JAVA] [Difficulty 2] [1940] 가랏! RC카! (0) 2022.11.16 [SWEA] [JAVA] [Difficulty 2] [1959] 두 개의 숫자열 (0) 2022.11.16 [SWEA] [JAVA] [Difficulty 2] [1979] 어디에 단어가 들어갈 수 있을까 (0) 2022.11.16 [SWEA] [JAVA] [Difficulty 2] [1284] 수도 요금 경쟁 (0) 2022.11.16