Advertisement
Spocoman

04. Bus

Nov 24th, 2023
543
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.83 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. int main() {
  7.     int busCount;
  8.     cin >> busCount;
  9.  
  10.     string bus, train;
  11.  
  12.     string* busTimes = new string[busCount];
  13.  
  14.     for (int i = 0; i < busCount; i++) {
  15.         cin >> busTimes[i];
  16.     }
  17.  
  18.     cin >> train;
  19.  
  20.     int index = 0,
  21.         diffTime = 0,
  22.         minDiff = 2147483647,
  23.         trainTime = stoi(train.substr(0, 2)) * 60 + stoi(train.substr(2));
  24.  
  25.     for (int i = 0; i < busCount; i++) {
  26.         diffTime = trainTime - (stoi(busTimes[i].substr(0, 2)) * 60 + stoi(busTimes[i].substr(2)));
  27.         if (diffTime < 0) {
  28.             index = i;
  29.             break;
  30.         }
  31.         else if (abs(diffTime) < minDiff) {
  32.             minDiff = abs(diffTime);
  33.             index = i + 1;
  34.         }
  35.     }
  36.  
  37.     cout << index << endl;
  38.     return 0;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement