Advertisement
Spocoman

01. Sum Seconds

Sep 1st, 2023 (edited)
655
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.30 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main() {
  6.     int firstTime, secondTime, thirdTime;
  7.     cin >> firstTime >> secondTime >> thirdTime;
  8.  
  9.     int total = firstTime + secondTime + thirdTime;
  10.     int minutes = total / 60;
  11.     int seconds = total % 60;
  12.  
  13.     if (seconds < 10) {
  14.         cout << minutes << ":0" << seconds << endl;
  15.     }
  16.     else {
  17.         cout << minutes << ":" << seconds << endl;
  18.     }
  19. }
  20.  
  21. Решение с тернарен оператор:
  22.  
  23. #include <iostream>
  24.  
  25. using namespace std;
  26.  
  27. int main() {
  28.     int firstTime, secondTime, thirdTime;
  29.     cin >> firstTime >> secondTime >> thirdTime;
  30.  
  31.     int total = firstTime + secondTime + thirdTime;
  32.     int minutes = total / 60;
  33.     int seconds = total % 60;
  34.  
  35.     cout << minutes << ":" << (seconds < 10 ? "0" : "") << seconds << endl;
  36.  
  37.     return 0;
  38. }
  39.  
  40. Решение със setW() и setfill() на библиотеката iomanip:
  41.  
  42. #include <iostream>
  43. #include <iomanip>
  44.  
  45. using namespace std;
  46.  
  47. int main() {
  48.     int firstTime, secondTime, thirdTime;
  49.     cin >> firstTime >> secondTime >> thirdTime;
  50.  
  51.     int total = firstTime + secondTime + thirdTime;
  52.     int minutes = total / 60;
  53.     int seconds = total % 60;
  54.  
  55.     cout << minutes << ':' << setw(2) << setfill('0') << seconds << endl;
  56.  
  57.     return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement