Guest User

Untitled

a guest
Oct 17th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. /**
  9. * Auto-generated code below aims at helping you parse
  10. * the standard input according to the problem statement.
  11. **/
  12. int main()
  13. {
  14.  
  15. int n; // the number of temperatures to analyse
  16. cin >> n; cin.ignore();
  17.  
  18. vector<int> temperatures;
  19. int current = 9999; // comparable value initialized with impossibly large value
  20.  
  21. for (int i = 0; i < n; i++) {
  22. int t; // a temperature expressed as an integer ranging from -273 to 5526
  23. cin >> t; cin.ignore();
  24. temperatures.push_back(t); // push values to vector
  25. }
  26.  
  27. if (!temperatures.empty()) // check, that actual data was given
  28. {
  29. for (auto itr : temperatures) // go though the vector
  30. {
  31. /* If the iterator is closer to 0 then current value
  32. ** or if the iterated value is same, but positive
  33. ** --> set current value to iterator.
  34. */
  35. if (abs(itr) < abs(current) || current == -abs(itr))
  36. current = itr;
  37. }
  38. }
  39. else // if no data was given (n = 0)
  40. {
  41. current = 0;
  42. }
  43. // Write an action using cout. DON'T FORGET THE "<< endl"
  44. // To debug: cerr << "Debug messages..." << endl;
  45.  
  46. cout << current << endl;
  47. }
  48.  
  49.  
  50. // Better solution: Thanks to CodinGames user Magaiti
  51.  
  52. /*
  53. #include <iostream>
  54. #include <iterator>
  55. #include <algorithm>
  56.  
  57. using namespace std;
  58.  
  59. bool closer_to_zero (int i, int j) {
  60. return (abs(i) < abs(j)) || (abs(i) == abs(j) && i > 0);
  61. }
  62.  
  63. int main() {
  64. istream_iterator<int> it(cin), eos;
  65. cout << (++it == eos ? 0 : *min_element (it, eos, closer_to_zero)) << endl;
  66. }
  67. */
Add Comment
Please, Sign In to add comment