Advertisement
Spocoman

11. Odd / Even Position

Sep 9th, 2023
1,112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.25 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main() {
  6.    
  7.     int n;
  8.     cin >> n;
  9.  
  10.     double oddSum = 0, oddMin = 1000000000.0, oddMax = -1000000000.0,
  11.         evenSum = 0, evenMin = 1000000000.0, evenMax = -1000000000.0, number;
  12.  
  13.     for (int i = 1; i <= n; i++) {
  14.         cin >> number;
  15.         if (i % 2 == 1) {
  16.             oddSum += number;
  17.             if (number < oddMin) {
  18.                 oddMin = number;
  19.             }
  20.             if (number > oddMax) {
  21.                 oddMax = number;
  22.             }
  23.         }
  24.         else {
  25.             evenSum += number;
  26.             if (number < evenMin) {
  27.                 evenMin = number;
  28.             }
  29.             if (number > evenMax) {
  30.                 evenMax = number;
  31.             }
  32.         }
  33.     }
  34.  
  35.     cout.setf(ios::fixed);
  36.     cout.precision(2);
  37.  
  38.     if (n == 0) {
  39.         cout << "OddSum=0.00, \nOddMin=No, \nOddMax=No,\n";
  40.     }
  41.     else {
  42.         cout << "OddSum=" << oddSum << ",\nOddMin="  << oddMin << ",\nOddMax=" << oddMax << ",\n";
  43.     }
  44.  
  45.     if (n <= 1) {
  46.         cout << "EvenSum=0.00, \nEvenMin=No, \nEvenMax=No\n";
  47.     }
  48.     else {
  49.         cout << "EvenSum=" << evenSum << ",\nEvenMin=" << evenMin << ",\nEvenMax=" << evenMax << "\n";
  50.     }
  51.  
  52.     return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement