Advertisement
DidiMilikina

11. Odd / Even Position

Oct 8th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.12 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <string>
  4. using namespace std;
  5.  
  6. int main() {
  7.     int n;
  8.     cin >> n;
  9.  
  10.     double a;
  11.     double oddmin = 10000.0, oddmax = -10000.0, oddsum = 0, evenmin = 10000.0, evenmax = -10000.0, evensum = 0;
  12.     bool haseven = false, hasodd = false;
  13.     if (n > 0) {
  14.         for (int i = 1; i <= n; i++)
  15.         {
  16.             cin >> a;
  17.             if (i % 2 == 0)
  18.             {
  19.                 if (a<evenmin) {
  20.                     evenmin = a;
  21.                 }
  22.                 if (a>evenmax)
  23.                 {
  24.                     evenmax = a;
  25.                 }
  26.                 evensum += a;
  27.                 haseven = true;
  28.             }
  29.             else
  30.             {
  31.                 if (a<oddmin) {
  32.                     oddmin = a;
  33.                 }
  34.                 if (a>oddmax)
  35.                 {
  36.                     oddmax = a;
  37.                 }
  38.                 oddsum += a;
  39.                 hasodd = true;
  40.             }
  41.         }
  42.  
  43.     }
  44.     cout << "OddSum=" << oddsum << ", ";
  45.     if (!hasodd) {
  46.         cout << "OddMin=" << "No, ";
  47.         cout << "OddMax=" << "No, ";
  48.     }
  49.     else
  50.     {
  51.         cout << "OddMin=" << oddmin << ", ";
  52.         cout << "OddMax=" << oddmax << ", ";
  53.     }
  54.     cout << "EvenSum=" << evensum << ", ";
  55.     if (!haseven) {
  56.         cout << "EvenMin=" << "No, ";
  57.         cout << "EvenMax=" << "No";
  58.     }
  59.     else
  60.     {
  61.         cout << "EvenMin=" << evenmin << ", ";
  62.         cout << "EvenMax=" << evenmax << endl;
  63.     }
  64.     return 0;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement