Advertisement
Lyubohd

Untitled

Jun 24th, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.97 KB | None | 0 0
  1. import java.text.DecimalFormat;
  2. import java.util.Scanner;
  3.  
  4. public class p11_OddEvenPosition {
  5.     public static void main(String[] args) {
  6.         Scanner scan = new Scanner(System.in);
  7.  
  8.         int n = Integer.parseInt(scan.nextLine());
  9.  
  10.         double oddSum = 0.0;
  11.         double minOdd = 1000000000.0;
  12.         double maxOdd = -1000000000.0;
  13.  
  14.         double evenSum = 0.0;
  15.         double minEven = 1000000000.0;
  16.         double maxEven = -1000000000.0;
  17.  
  18.         DecimalFormat format = new DecimalFormat("0.##");
  19.  
  20.         for (int i = 1; i <= n; i++) {
  21.             double num = Double.parseDouble(scan.nextLine());
  22.             if (i % 2 == 0) {
  23.                 evenSum += num;
  24.                 if (num >= maxEven) {
  25.                     maxEven = num;
  26.                 }
  27.                 if (num <= minEven) {
  28.                     minEven = num;
  29.                 }
  30.             } else if (i % 2 != 0) {
  31.                 oddSum += num;
  32.                 if (num >= maxOdd) {
  33.                     maxOdd = num;
  34.                 }
  35.                 if (num <= minOdd) {
  36.                     minOdd = num;
  37.                 }
  38.             }
  39.         }
  40.         System.out.printf("OddSum=%s,%n", format.format(oddSum));
  41.         if (minOdd == 1000000000.0) {
  42.             System.out.println("OddMin= No,");
  43.         } else
  44.             System.out.printf("OddMin=%s,%n", format.format(minOdd));
  45.         if (maxOdd == -1000000000.0) {
  46.             System.out.println("OddMax= No,");
  47.         } else
  48.             System.out.printf("OddMax=%s,%n", format.format(maxOdd));
  49.         System.out.printf("EvenSum=%s,%n", format.format(evenSum));
  50.         if (minEven == 1000000000.0) {
  51.             System.out.println("EvenMin= No,");
  52.         } else
  53.             System.out.printf("EvenMin=%s,%n", format.format(minEven));
  54.         if (maxEven == -1000000000.0) {
  55.             System.out.println("EvenMax= No,");
  56.         } else
  57.             System.out.printf("EvenMax=%s", format.format(maxEven));
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement