Advertisement
Guest User

Untitled

a guest
May 25th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <sstream>
  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. int n; // the number of temperatures to analyse
  15. cin >> n; cin.ignore();
  16. string temps; // the n temperatures expressed as integers ranging from -273 to 5526
  17. getline(cin, temps);
  18.  
  19.  
  20. // std::string input;
  21. // std::getline(std::cin, input);
  22.  
  23. std::stringstream stream(temps);
  24.  
  25. int pmin = 1000;
  26. int nmax = -1000;
  27. int result;
  28. while(1){
  29.  
  30. int n;
  31. stream >> n;
  32. if(!stream)
  33. break;
  34. std::cout << "Found integer: " << n << "\n";
  35.  
  36. //find smallest positive number
  37. if(n>=0 && n < pmin)
  38. {
  39. pmin = n;
  40. cout << "pmin="<< pmin << endl;
  41. }
  42. else if(n<0 && n > nmax) // find biggest negative integer
  43. {
  44. nmax = n;
  45. cout << "nmax="<< nmax << endl;
  46. }
  47.  
  48. } //end of while loop
  49.  
  50.  
  51. //check which is closer to 0 from pmin and nmax
  52. if (abs(pmin) < abs(nmax))
  53. {
  54. result = pmin;
  55. }
  56. else if(abs(pmin)>abs(nmax))
  57. {
  58. result = nmax;
  59. }
  60. else
  61. {
  62. result = pmin;
  63. }
  64.  
  65. cout << result << endl;
  66. //cout << temps << endl;
  67. //cout << "result" << endl;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement