Algorithm
Event Time
Adapted by Neilor Tonin, URI Brazil
Peter is organizing an event in his University. The event will be in April month, beginning and finishing within April month. The problem is: Peter wants to calculate the event duration in seconds, knowing obviously the begin and the end time of the event.
You know that the event can take from few seconds to some days, so, you must help Peter to compute the total time corresponding to duration of the event.
Input
The first line of the input contains information about the beginning day of the event in the format: “Dia xx”. The next line contains the start time of the event in the format presented in the sample input. Follow 2 input lines with same format, corresponding to the end of the event.
Output
Your program must print the following output, one information by line, considering that if any information is null for example, the number 0 must be printed in place of W, X, Y or Z:
W dia(s)
X hora(s)
Y minuto(s)
Z segundo(s)
Obs: Consider that the event of the test case have the minimum duration of one minute. “dia” means day, “hora” means hour, “minuto” means minute and “Segundo” means second in Portuguese.
Input Sample | Output Sample |
Dia 5 |
3 dia(s) |
Code Examples
#1 Code Example with C Programming
Code -
C Programming
#include <stdio.h>
int main()
{
char a[4], b[2],c[2],d[2],e[2],f[4];
int i,j,k,l,m,n,p,q,r,s,t,u,v,w,x,y,z,h[5];
scanf("%s%d", a,&i);
scanf("%d%s%d%s%d", &j,&b,&k,&c,&l);
scanf("%s%d", &f,&p);
scanf("%d%s%d%s%d", &q, &d,&r,&e,&s);
m=60-l+s;
n=(60-k-1+r)*60;
t=(24-1-j+q)*3600;
u=(p-i-1)*86400;
v=m+n+t+u;
h[0]=v/86400;
printf("%d dia(s)n", h[0]);
h[0]=v%86400;
h[1]=h[0]/3600;
printf("%d hora(s)n", h[1]);
h[1]=h[0]%3600;
h[2]=h[1]/60;
printf("%d minuto(s)n", h[2]);
h[2]=h[1]%60;
printf("%d segundo(s)n", h[2]);
return 0;
}
Copy The Code &
Try With Live Editor
Input
Output
#2 Code Example with C++ Programming
Code -
C++ Programming
#include <iostream>
using namespace std;
int main()
{
string trash;
int dia_i, dia_f, hora_i, hora_f, minuto_i, minuto_f, segundo_i, segundo_f;
int ctr_d = 0, ctr_h = 0, ctr_m = 0, ctr_s = 0;
bool ver_h = false, ver_m = false, ver_s = false;
cin >> trash >> dia_i;
cin >> hora_i >> trash >> minuto_i >> trash >> segundo_i;
cin >> trash >> dia_f;
cin >> hora_f >> trash >> minuto_f >> trash >> segundo_f;
if(hora_i > hora_f)
ver_h = true;
if(minuto_i > minuto_f)
ver_m = true;
if(segundo_i > segundo_f)
ver_s = true;
while(dia_i != dia_f)
{
ctr_d++;
dia_i++;
}
while(hora_i != hora_f)
{
ctr_h++;
hora_i++;
if(hora_i == 25)
hora_i = 1;
}
while(minuto_i != minuto_f)
{
ctr_m++;
minuto_i++;
if(minuto_i == 61)
minuto_i = 1;
}
while(segundo_i != segundo_f)
{
ctr_s++;
segundo_i++;
if(segundo_i == 61)
segundo_i = 1;
}
if(ver_h == true)
ctr_d--;
if(ver_m == true)
ctr_h--;
if(ver_s == true)
ctr_m--;
cout << ctr_d << " dia(s)" << endl;
cout << ctr_h << " hora(s)" << endl;
cout << ctr_m << " minuto(s)" << endl;
cout << ctr_s << " segundo(s)" << endl;
return 0;
}
Copy The Code &
Try With Live Editor
Input
Output
#3 Code Example with Java Programming
Code -
Java Programming
import java.io.IOException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException {
Scanner sc =new Scanner(System.in);
String string;
int day_i, day_f, hour_i, hour_f, minute_i, minute_f, second_i, second_f;
int day = 0, hour = 0, minute = 0, second = 0;
boolean bool_h = false, bool_m = false, bool_s = false;
//----------------------
string =sc.next();
day_i =sc.nextInt();
//----------------------
hour_i =sc.nextInt();
string =sc.next();
minute_i =sc.nextInt();
string =sc.next();
second_i =sc.nextInt();
//----------------------
string =sc.next();
day_f =sc.nextInt();
//----------------------
hour_f =sc.nextInt();
string =sc.next();
minute_f =sc.nextInt();
string =sc.next();
second_f =sc.nextInt();
//----------------------
if(hour_i > hour_f)
bool_h = true;
if(minute_i > minute_f)
bool_m = true;
if(second_i > second_f)
bool_s = true;
while(day_i != day_f)
{
day++;
day_i++;
}
while(hour_i != hour_f)
{
hour++;
hour_i++;
if(hour_i == 25)
hour_i = 1;
}
while(minute_i != minute_f)
{
minute++;
minute_i++;
if(minute_i == 61)
minute_i = 1;
}
while(second_i != second_f)
{
second++;
second_i++;
if(second_i == 61)
second_i = 1;
}
if(bool_h == true)
day--;
if(bool_m == true)
hour--;
if(bool_s == true)
minute--;
System.out.print(day+" dia(s)n");
System.out.print(hour+" hora(s)n");
System.out.print(minute+" minuto(s)n");
System.out.print(second+" segundo(s)n");
}
}
Copy The Code &
Try With Live Editor
Input
#4 Code Example with Python Programming
Code -
Python Programming
dia,date1=input().split()
date1 = int(date1)
h1,m1,s1=map(int,input().split(":"))
dia,date2=input().split()
date2 = int(date2)
h2,m2,s2=map(int,input().split(":"))
s = s2 - s1
m = m2 - m1
h = h2 - h1
d = date2 - date1
if(s<0):
s+=60
m-=1
if(m<0):
m+=60
h-=1
if(h<0):
h+=24
d-=1
print(f"{d} dia(s)")
print(f"{h} hora(s)")
print(f"{m} minuto(s)")
print(f"{s} segundo(s)">
Copy The Code &
Try With Live Editor
Input
Output