Advertisement
Spocoman

03. Time + 15 Minutes

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