Advertisement
Spocoman

11. Odd / Even Position

Aug 28th, 2024
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.55 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class OddEvenPosition {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.         int n = Integer.parseInt(scanner.nextLine());
  7.         double oddMin = 1000000000.0;
  8.         double oddMax = -1000000000.0;
  9.         double evenMin = 1000000000.0;
  10.         double evenMax = -1000000000.0;
  11.         double oddSum = 0, evenSum = 0;
  12.  
  13.         for (int i = 1; i <= n; i++) {
  14.             double currentNum = Double.parseDouble(scanner.nextLine());
  15.             if (i % 2 == 1) {
  16.                 oddSum += currentNum;
  17.                 if (currentNum < oddMin) {
  18.                     oddMin = currentNum;
  19.                 }
  20.                 if (currentNum > oddMax) {
  21.                     oddMax = currentNum;
  22.                 }
  23.             } else {
  24.                 evenSum += currentNum;
  25.                 if (currentNum < evenMin) {
  26.                     evenMin = currentNum;
  27.                 }
  28.                 if (currentNum > evenMax) {
  29.                     evenMax = currentNum;
  30.                 }
  31.             }
  32.         }
  33.  
  34.         if (n == 0) {
  35.             System.out.println("OddSum=0.00,\nOddMin=No,\nOddMax=No,");
  36.         } else {
  37.             System.out.printf("OddSum=%.2f,\nOddMin=%.2f,\nOddMax=%.2f,\n", oddSum, oddMin, oddMax);
  38.         }
  39.  
  40.         if (n <= 1) {
  41.             System.out.println("EvenSum=0.00,\nEvenMin=No,\nEvenMax=No");
  42.         } else {
  43.             System.out.printf("EvenSum=%.2f,\nEvenMin=%.2f,\nEvenMax=%.2f\n", evenSum, evenMin, evenMax);
  44.         }
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement