Advertisement
CR7CR7

oddeven

Mar 20th, 2023
585
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.03 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3.  
  4. public class Main {
  5.   public static void main(String[] args) {
  6.     Scanner sc = new Scanner(System.in);
  7.  
  8.     int totalNumbersForInput = sc.nextInt();
  9.  
  10.     ArrayList<Double> allNumbers = new ArrayList<>();
  11.     ArrayList<Double> oddNumbers = new ArrayList<>();
  12.     ArrayList<Double> evenNumbers = new ArrayList<>();
  13.  
  14.     for (int i = 0; i < totalNumbersForInput; i++) {
  15.       allNumbers.add(sc.nextDouble());
  16.     }
  17.  
  18.     int check = 0;
  19.  
  20.     for (double number : allNumbers) {
  21.       if (check % 2 == 0) {
  22.         oddNumbers.add(number);
  23.       } else {
  24.         evenNumbers.add(number);
  25.       }
  26.       check++;
  27.     }
  28.  
  29.     double evenSum = 0;
  30.     for (double number : evenNumbers) {
  31.       evenSum += number;
  32.     }
  33.  
  34.     double oddSum = 0;
  35.     for (double number : oddNumbers) {
  36.       oddSum += number;
  37.     }
  38.  
  39.     evenNumbers.sort(null);
  40.     oddNumbers.sort(null);
  41.  
  42.     if (totalNumbersForInput == 1) {
  43.       System.out.printf("OddSum=%.2f,\n", oddSum);
  44.       System.out.printf("OddMin=%.2f,\n", oddNumbers.get(0));
  45.       System.out.printf("OddMax=%.2f,\n", oddNumbers.get(oddNumbers.size() - 1));
  46.       System.out.printf("EvenSum=%.2f,\n", evenSum);
  47.       System.out.println("EvenMin=No,");
  48.       System.out.println("EvenMax=No");
  49.     } else if (totalNumbersForInput == 0) {
  50.         System.out.printf("OddSum=0.00,\n");
  51.         System.out.printf("OddMin=No,\n");
  52.         System.out.printf("OddMax=No,\n");
  53.         System.out.printf("EvenSum=0.00,\n");
  54.         System.out.println("EvenMin=No,");
  55.         System.out.println("EvenMax=No");
  56.     } else {
  57.         System.out.printf("OddSum=%.2f,\n", oddSum);
  58.         System.out.printf("OddMin=%.2f,\n", oddNumbers.get(0));
  59.         System.out.printf("OddMax=%.2f,\n", oddNumbers.get(oddNumbers.size() - 1));
  60.         System.out.printf("EvenSum=%.2f,\n", evenSum);
  61.         System.out.printf("EvenMin=%.2f,\n", evenNumbers.get(0));
  62.         System.out.printf("EvenMax=%.2f\n", evenNumbers.get(evenNumbers.size() - 1));
  63.     }
  64.   }
  65. }
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement