ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 시간 차 구하기
    공부/짜잘한 알고리즘 문제 2019. 6. 21. 12:56
    #include<iostream>
    #include<algorithm>
    
    
    using namespace std;
    
    int main() {
    	char *temp=NULL;
    	int h = 0, m = 0, s = 0;
    	char time1[100], time2[100];
    	int gap = 0, t1 = 0, t2 = 0, rh = 0, rm = 0, rs = 0;
    	char ch[10], cm[10], cs[10];
    
    
    	cin.getline(time1, 100);
    
    	strtok_s(time1, " ",&temp);
    	h = atoi(strtok_s(temp, ":", &temp));
    	m = atoi(strtok_s(temp, ":", &temp));
    	s = atoi(strtok_s(temp, ":", &temp));
    
    	t1 = h * 3600 + m * 60 + s;
    
    	cin.getline(time2, 100);
    	strtok_s(time2, " ", &temp);
    	h = atoi(strtok_s(temp, ":", &temp));
    	m = atoi(strtok_s(temp, ":", &temp));
    	s = atoi(strtok_s(temp, ":", &temp));
    
    	t2 = h * 3600 + m * 60 + s;
    
    	gap = t2 - t1;
    
    	if (time1[0] != time2[0]) {
    		gap += 12 * 3600;
    	}
    
    	rh = gap/3600;
    	gap -= rh * 3600;
    	rm = gap/60;
    	gap -= rm * 60;
    	rs = gap;
    
    	if (rh < 10) 
    		sprintf_s(ch, sizeof(ch), "0%d", rh);
    	else
    		sprintf_s(ch, sizeof(ch), "%d", rh);
    
    	if (rm < 10)
    		sprintf_s(cm, sizeof(cm), "0%d", rm);
    	else
    		sprintf_s(cm, sizeof(cm), "%d", rm);
    
    	if (rs < 10)
    		sprintf_s(cs, sizeof(cs), "0%d", rs);
    	else
    		sprintf_s(cs, sizeof(cs), "%d", rs);
    
    
    	cout << ch<<":" << cm << ":" << cs;
    	return 0;
    }

    댓글

Designed by Tistory.