fork(1) download
  1.  
  2.  
  3. #include <iostream>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. // function to convert time in format HH:MM:SS to seconds
  9. int timeToSeconds(string time) {
  10. int hours = stoi(time.substr(0, 2));
  11. int minutes = stoi(time.substr(3, 2));
  12. int seconds = stoi(time.substr(6, 2));
  13. return hours * 3600 + minutes * 60 + seconds;
  14. }
  15.  
  16. int main() {
  17. // read Iftar times for each day of the week
  18. int iftarTimes[7];
  19. for (int i = 0; i < 7; i++) {
  20. string day, time;
  21. cin >> day >> time;
  22. iftarTimes[i] = timeToSeconds(time);
  23. }
  24.  
  25. // read number of questions
  26. int q;
  27. cin >> q;
  28.  
  29. // answer each question
  30. while (q--) {
  31. string day, time;
  32. cin >> day >> time;
  33. int currentSeconds = timeToSeconds(time);
  34.  
  35. // find index of current day in the week
  36. int currentDayIndex;
  37. if (day == "Saturday") currentDayIndex = 0;
  38. else if (day == "Sunday") currentDayIndex = 1;
  39. else if (day == "Monday") currentDayIndex = 2;
  40. else if (day == "Tuesday") currentDayIndex = 3;
  41. else if (day == "Wednesday") currentDayIndex = 4;
  42. else if (day == "Thursday") currentDayIndex = 5;
  43. else /*if (day == "Friday")*/ currentDayIndex = 6;
  44.  
  45. // calculate seconds until next Iftar
  46. int secondsUntilIftar;
  47. if (currentSeconds >= iftarTimes[currentDayIndex]) {
  48. // next Iftar is tomorrow
  49. secondsUntilIftar = 24 * 3600 - currentSeconds + iftarTimes[(currentDayIndex + 1) % 7];
  50. } else {
  51. // next Iftar is today
  52. secondsUntilIftar = iftarTimes[currentDayIndex] - currentSeconds;
  53. }
  54.  
  55. // output answer
  56. cout << secondsUntilIftar << endl;
  57. }
  58.  
  59. return 0;
  60. }
  61.  
  62.  
Success #stdin #stdout 0.01s 5520KB
stdin
Saturday 06:00:00
Sunday 06:01:00
Monday 06:02:00
Tuesday 06:03:00
Wednesday 06:04:00
Thursday 06:05:00
Friday 06:06:00
2
Saturday 05:52:20
Thursday 06:05:00
stdout
460
86460