알고리즘/삼성 SW expert Academy

[SWEA] 7732. 시간 개념_JAVA

뇌장하드 2022. 5. 28. 20:22

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

 

SW Expert Academy

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

swexpertacademy.com

 

시간문제는 초로 합쳐서 계산해주기

import java.util.Scanner;
import java.io.FileInputStream;

class Solution
{
	public static void main(String args[]) throws Exception
	{
		Scanner sc = new Scanner(System.in);
		int T;
		T=sc.nextInt();
        sc.nextLine();
		for(int test_case = 1; test_case <= T; test_case++)
		{
            String[] first=sc.nextLine().split(":");
            String[] sec=sc.nextLine().split(":");
            
           	int first_h=Integer.parseInt(first[0])*3600+Integer.parseInt(first[1])*60+Integer.parseInt(first[2]);
             int sec_h=Integer.parseInt(sec[0])*3600+Integer.parseInt(sec[1])*60+Integer.parseInt(sec[2]);
            if(sec_h-first_h<0){
                sec_h+=24*3600;
            } 
            int result=sec_h-first_h;
            int hour=result/3600;
            int min=(result-hour*3600)/60;
            int se=(result-hour*3600-min*60);
            String hour1=String.valueOf(hour);
            String min1=String.valueOf(min);
            String sec1=String.valueOf(se);
            
            if(hour<10){
                hour1="0"+hour1;   
            }
            if(min<10){
                min1="0"+min1;
            }
             if(se<10){
                sec1="0"+sec1;
            }
            System.out.println("#"+test_case+" "+hour1+":"+min1+":"+sec1);
		}
	}
}