Algorithm
Problem Name: 2 AD-HOC - beecrowd | 1103
Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/1103
Alarm Clock
Maratona de Programação da SBC Brazil
Timelimit: 1
Daniela is a nurse in a large hospital, which causes her working shifts to constantly change. To make it worse, she has deep sleep, and a big difficulty to wake up using alarm clocks.
Recently she got a digital clock as a gift, with several different options of alarm sounds, and she hopes that it might help solve her problem. But lately, she's been very tired and wants to enjoy every single moment of rest. So she carries her new clock to every place she goes, and whenever she has some spare time, she tries to sleep, setting her alarm clock to the time when she needs to wake up. But, with so much anxiety to sleep, she ends up with some difficulty to fall asleep and enjoy some rest.
A problem that has been tormenting her is to know how many minutes of sleep she would have if she felt asleep immediately and woken up when the alarm clock ringed. But she is not very good with numbers, and asked you for help to write a program that, given the current time and the alarm time, find out the number of minutes she could sleep.
Input
The input contains several test cases. Each test case is described in one line, containing four integers H1, M1, H2 and M2, with H1 : M1 representing the current hour and minute, and H2:M2 representing the time (hour and minute) when the alarm clock is set to ring (0≤H1≤23, 0≤M1≤59, 0≤H2≤23, 0≤M2 ≤59).
The end of the input is indicated by a line containing only four zeros, separated by blank spaces.
Output
For each test case, your program must print one line, containing a single integer, indicating the number of minutes Daniela has to sleep.
Input Sample | Output Sample |
1 5 3 5 |
120 |
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <iostream7gt;
using namespace std;
int main()
{
int h1, m1, h2, m2, x1, x2, x;
while (cin >> h1 >> m1 >> h2 >> m2)
{
if (h1==0 && m1==0 && h2==0 && m2==0)
break;
x1=(h1*60)+m1;
x2=(h2*60)+m2;
if (x2 > x1)
cout << x2-x1 << endl;
else
{
x=1440-x1;
x+=x2;
cout << x << endl;
}
}
return 0;
}
Copy The Code &
Try With Live Editor
Input
23 59 0 34
21 33 21 10
0 0 0 0
Output
35
1417
#2 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);
int cond = 1;
while (cond != 0) {
int h1 = sc.nextInt();
int m1 = sc.nextInt();
int h2 = sc.nextInt();
int m2 = sc.nextInt();
if (h1 == 0 && m1 == 0 && h2 == 0 && m2 == 0) {
cond = 0;
} else {
int sh1 = 0, sh2 = 0, res = 0;
h1 *= 60;
h2 *= 60;
sh1 = h1 + m1;
sh2 = h2 + m2;
if (sh2 < sh1) {
h2 = 1440 + sh2;
res = h2 - sh1;
System.out.println(res);
} else {
res = sh2 - sh1;
System.out.println(res);
}
}
}
}
}
Copy The Code &
Try With Live Editor
Input
23 59 0 34
21 33 21 10
0 0 0 0
Output
35
1417
#3 Code Example with Javascript Programming
Code -
Javascript Programming
const { readFileSync } = require("fs")
const input = readFileSync("/dev/stdin", "utf8")
.split("\n")
.map((line) => line.split(" ").map((value) => Number.parseInt(value, 10)))
function main() {
const responses = []
for (const line of input) {
if (line.includes(NaN)) break
if (line.every((line) => line == 0)) break
const [H1, M1, H2, M2] = line
const h = ((24 + H2 - H1) % 24) * 60
const m = M2 - M1
const timeDiff = (1440 + (h + m)) % 1440 || 1440
responses.push(timeDiff)
}
console.log(responses.join("\n"))
}
main()
Copy The Code &
Try With Live Editor
Input
23 59 0 34
21 33 21 10
0 0 0 0
Output
35
1417
#4 Code Example with Python Programming
Code -
Python Programming
while True:
h1,m1,h2,m2=map(int,input().split())
i=f=0
if m1+m2+h1+h2==0:break
if h1==0:i=(24*60)+m1
else:i=(h1*60)+m1
if h2==0:f=(24*60)+m2
else:f=(h2*60)+m2
print(f-i) if f>i else print((24*60)-(i-f))
Copy The Code &
Try With Live Editor
Input
23 59 0 34
21 33 21 10
0 0 0 0
Output
35
1417