Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- #include <algorithm>
- using namespace std;
- /**
- * Auto-generated code below aims at helping you parse
- * the standard input according to the problem statement.
- **/
- int main()
- {
- int n; // the number of temperatures to analyse
- cin >> n; cin.ignore();
- vector<int> temperatures;
- int current = 9999; // comparable value initialized with impossibly large value
- for (int i = 0; i < n; i++) {
- int t; // a temperature expressed as an integer ranging from -273 to 5526
- cin >> t; cin.ignore();
- temperatures.push_back(t); // push values to vector
- }
- if (!temperatures.empty()) // check, that actual data was given
- {
- for (auto itr : temperatures) // go though the vector
- {
- /* If the iterator is closer to 0 then current value
- ** or if the iterated value is same, but positive
- ** --> set current value to iterator.
- */
- if (abs(itr) < abs(current) || current == -abs(itr))
- current = itr;
- }
- }
- else // if no data was given (n = 0)
- {
- current = 0;
- }
- // Write an action using cout. DON'T FORGET THE "<< endl"
- // To debug: cerr << "Debug messages..." << endl;
- cout << current << endl;
- }
- // Better solution: Thanks to CodinGames user Magaiti
- /*
- #include <iostream>
- #include <iterator>
- #include <algorithm>
- using namespace std;
- bool closer_to_zero (int i, int j) {
- return (abs(i) < abs(j)) || (abs(i) == abs(j) && i > 0);
- }
- int main() {
- istream_iterator<int> it(cin), eos;
- cout << (++it == eos ? 0 : *min_element (it, eos, closer_to_zero)) << endl;
- }
- */
Add Comment
Please, Sign In to add comment