fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. typedef int WORD;
  7.  
  8. typedef struct _SYSTEMTIME {
  9. WORD wYear;
  10. WORD wMonth;
  11. WORD wDayOfWeek;
  12. WORD wDay;
  13. WORD wHour;
  14. WORD wMinute;
  15. WORD wSecond;
  16. WORD wMilliseconds;
  17. } SYSTEMTIME;
  18.  
  19. class ClosestTo {
  20. int minute_now;
  21. int abs_minute(const SYSTEMTIME& t) const {
  22. return 60 * (24 * t.wDayOfWeek + t.wHour) + t.wMinute;
  23. }
  24. int diff_to_now(const SYSTEMTIME& t) const {
  25. int res = abs_minute(t) - minute_now;
  26. // Has it passed?
  27. if (res < 0) {
  28. // Move to next week
  29. res += 7*24*60;
  30. }
  31. return res;
  32. }
  33. public:
  34. ClosestTo(const SYSTEMTIME& now)
  35. : minute_now(abs_minute(now)) {
  36. }
  37. bool operator() (const SYSTEMTIME& lhs, const SYSTEMTIME& rhs) const {
  38. return diff_to_now(lhs) < diff_to_now(rhs);
  39. }
  40. };
  41.  
  42. int main() {
  43. SYSTEMTIME m_MatchTime[3];
  44.  
  45. // Monday: 00:00
  46. m_MatchTime[0].wDayOfWeek = 1;
  47. m_MatchTime[0].wHour = 22;
  48. m_MatchTime[0].wMinute = 4;
  49.  
  50. // Sunday: 01:00
  51. m_MatchTime[1].wDayOfWeek = 4;
  52. m_MatchTime[1].wHour = 1;
  53. m_MatchTime[1].wMinute = 0;
  54.  
  55. // Wednesday: 15:30
  56. m_MatchTime[2].wDayOfWeek = 6;
  57. m_MatchTime[2].wHour = 15;
  58. m_MatchTime[2].wMinute = 30;
  59.  
  60. // Sunday 23:00
  61. SYSTEMTIME cTime;
  62. cTime.wDayOfWeek = 3;
  63. cTime.wHour = 14;
  64. cTime.wMinute = 5;
  65.  
  66. ClosestTo cmp(cTime);
  67. sort(m_MatchTime, m_MatchTime+3, cmp);
  68.  
  69. SYSTEMTIME &nearest = m_MatchTime[0];
  70. cout << "CurrentTime\n"
  71. << "Day:" << cTime.wDayOfWeek
  72. << "Hour:" << cTime.wHour
  73. << "Min:" << cTime.wMinute
  74.  
  75. << "\nDay:" << nearest.wDayOfWeek
  76. << "Hour:" << nearest.wHour
  77. << "Min:" << nearest.wMinute << endl;
  78. return 0;
  79. }
Success #stdin #stdout 0.01s 2728KB
stdin
Standard input is empty
stdout
CurrentTime
Day:3Hour:14Min:5
Day:4Hour:1Min:0