Created
September 27, 2021 18:02
-
-
Save rendon/6f80eda397c7b7000dda120519731146 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
using std::vector; | |
void printAnswer(int frequency, int station) { | |
if (frequency < station) { | |
std::cout << "adelante " << station - frequency << "\n"; | |
} else if (frequency > station) { | |
std::cout << "atras " << frequency - station << "\n"; | |
} | |
} | |
void findNearestStation(int frequency, vector<int> const & stations) { | |
if (frequency < stations[0]) { | |
printAnswer(frequency, stations[0]); | |
return; | |
} | |
unsigned size = stations.size(); | |
if (frequency > stations[size-1]) { | |
printAnswer(frequency, stations[size-1]); | |
return; | |
} | |
for (unsigned idx = 0; idx < size; idx++) { | |
if (frequency == stations[idx]) { | |
if (idx == 0) { | |
printAnswer(stations[0], stations[1]); | |
return; | |
} | |
if (idx == size - 1) { | |
printAnswer(frequency, stations[size-2]); | |
return; | |
} | |
} | |
} | |
for (int idx = 0; idx + 1 < size; idx++) { | |
int left = frequency - stations[idx]; | |
int right = stations[idx+1] - frequency; | |
if (frequency < stations[idx] || frequency > stations[idx+1]) { | |
continue; | |
} | |
if (right <= left) { | |
printAnswer(frequency, stations[idx+1]); | |
} else { | |
printAnswer(frequency, stations[idx]); | |
} | |
} | |
} | |
int main() { | |
int min = 540; | |
int max = 1520; | |
vector<int> stations{580, 980, 1190, 1250, 1420}; | |
int frequency; | |
std::cin >> frequency; | |
if (frequency < min || frequency > max) { | |
std::cout << "error\n"; | |
} else { | |
findNearestStation(frequency, stations); | |
} | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment