View Code of Problem 56

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;

int main()
{
	int h1, m1, s1;
	int h2, m2, s2;
	int a, b, c;
	scanf("%d:%d:%d", &h1, &m1, &s1);
	scanf("%d:%d:%d", &h2, &m2, &s2);
	if (h1 * 3600 + m1 * 60 + s1 < h2 * 3600 + m2 * 60 + s2)
	{
		a = h2 - h1, b = m2 - m1, c = s2 - s1;
		if (c < 0)
		{
			c = 60 + c;
			b--;
		}
		if (b < 0)
		{
			b = b + 60;
			a--;
		}
	}
	else
	{
		a = h1 - h2, b = m1 - m2, c = s1 - s2;
		if (c < 0)
		{
			c = 60 + c;
			b--;
		}
		if (b < 0)
		{
			b = b + 60;
			a--;
		}
	}
	printf("%d:%02d:%02d", a, b, c);
    return 0;
}

Double click to view unformatted code.


Back to problem 56