URI Online Judge | 1061
Event Time
Adapted by Neilor Tonin, URI Brazil
Timelimit: 1
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, in seconds, 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.
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 08 : 12 : 23 Dia 9 06 : 13 : 23 | 3 dia(s) 22 hora(s) 1 minuto(s) 0 segundo(s) |
URI 1061 Solution in C language (accepted):
#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;
}
URI Solution in Java Language (accepted):
package URI_Problems_solution;
import java.util.Scanner;
public class URI_1061 {
public static void main(String[] args) {
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"); }}
Solution in C++ language:(Accepted)
#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;
}
URI 1061 C Code Explanation:
First take the necessary input as given format in these lines,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);
Then,
We need to find the difference between from one time to another.
First, change every variable or number to seconds and add them like this, Total second in first is:
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;
Now come to logic -
To convert day to second we need to multiply it
1 day = 24 hour * 60 minutes * 60 seconds = 86,400 seconds
1 hour = 60 minutes * 60 seconds = 3600 seconds
1 minute = 60 seconds
1 second = 1 second
So, the first Time In Day is =
h[0]=v/86400; printf("%d dia(s)\n", h[0]);
And now the work for hour is here-
h[0]=v%86400; h[1]=h[0]/3600; printf("%d hora(s)\n", h[1]);
Divide the total time by 86400 and its divide result is day as usual but we need the hour.
So, its reminder(%) time is in the second time and so again divide it by 3600 and get the hour time.
Got clear may be.
How to find minute:
Just take reminder after dividing 3600 and then take division of 60.
h[1]=h[0]%3600; h[2]=h[1]/60; printf("%d minuto(s)\n", h[2]);
Find Second:
h[2]=h[1]%60; printf("%d segundo(s)\n", h[2]);
[Note: Update for the request of Lighting Moon]
In this problem, you have to know some basic mathematics of second, minute, hour and days functionality. Just try to solve this problem at first in your logic.
Be aware that self logic is the best logic, I think so ever. And then try to submit.
If you face any problem further to this problem knock me
Email : manirujjamanakash@gmail.com
Or me at facebook - https://www.facebook.com/maniruzzaman.akash/
. or comment here.I'll help you as I can.
#include
ReplyDeleteint main()
{
int a,b,c,d,w,x,y,z,e,f,g,h,i,j,k,l;
scanf("%*s %d",&a);
scanf("%d %*s ",&b);
scanf("%d %*s ",&c);
scanf("%d",&d);
scanf("%*s %d",&w);
scanf("%d %*s ",&x);
scanf("%d %*s ",&y);
scanf("%d",&z);
if(zc)
k=(y-1)-c;
else if(z>d && yb)
f=x-1-b;
else if(y>c && x=a)
e=(w-1)-a;
else if (w>=a && x<b)
e=w-a;
else e=w-a;
printf("%d dia(s)\n",e);
printf("%d hora(s)\n",f);
printf("%d minuto(s)\n",k);
printf("%d segundo(s)\n",j);
return 0;
}
made by boss Raheeeeeeeeeeeeeeeeeeeeee