Grade 10 · The struct typeCitește în română
Adding durations (hours, minutes, seconds)
Original: Adunarea duratelor (ore, minute, secunde)
The idea
A duration is kept in a structure with three fields. To add easily, we turn the duration into seconds (h * 3600 + m * 60 + s), add the numbers and then rebuild the structure: the hours are total / 3600, the minutes (total % 3600) / 60 and the seconds total % 60. n durations are read and the total duration is printed.
Original description (Romanian)
O durată se ține într-o structură cu trei câmpuri. Ca să adunăm ușor, transformăm durata în secunde (h * 3600 + m * 60 + s), adunăm numerele și apoi refacem structura: orele sunt total / 3600, minutele (total % 3600) / 60, iar secundele total % 60. Se citesc n durate și se afișează durata totală.
- Complexity
- O(n)
- Grade
- Grade 10 (Clasa a X-a)
- Chapter
- The struct type
Examples
Input
3 1 45 30 0 20 45 2 0 50
Expected output
4 7 5
The code
Comments are in Romanian, as in the original collection. Edit freely: you can reset at any time.
/*
* Titlu: Adunarea duratelor (ore, minute, secunde)
* Clasa: 10
* Capitol: Tipul struct
* Descriere: O durată se ține într-o structură cu trei câmpuri. Ca să adunăm
* ușor, transformăm durata în secunde (h * 3600 + m * 60 + s), adunăm
* numerele și apoi refacem structura: orele sunt total / 3600, minutele
* (total % 3600) / 60, iar secundele total % 60. Se citesc n durate și
* se afișează durata totală.
* Complexitate: O(n)
*
* Exemplu
* Intrare:
* 3
* 1 45 30
* 0 20 45
* 2 0 50
* Ieșire: 4 7 5
*/
#include <iostream>
using namespace std;
struct Durata
{
int ore, minute, secunde;
};
long long inSecunde(Durata d)
{
return d.ore * 3600LL + d.minute * 60 + d.secunde;
}
Durata dinSecunde(long long total)
{
Durata d;
d.ore = total / 3600;
d.minute = total % 3600 / 60;
d.secunde = total % 60;
return d;
}
int main()
{
int n;
cin >> n;
long long total = 0;
for (int i = 0; i < n; i++)
{
Durata d;
cin >> d.ore >> d.minute >> d.secunde;
total += inSecunde(d);
}
Durata rez = dinSecunde(total);
cout << rez.ore << ' ' << rez.minute << ' ' << rez.secunde << '\n';
return 0;
}
Runs on a real g++ (C++17, -O2) through Compiler Explorer. Keep runs short; there's a time limit.