borkins

11. Odd / Even Position

Mar 30th, 2017
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.79 KB | None | 0 0
  1. /**
  2.  * Project: Loops - created by borkins on 2017-03-30.
  3.  */
  4.  
  5. import java.text.DecimalFormat;
  6. import java.util.Scanner;
  7.  
  8. public class _11_Odd_Even_Position
  9. {
  10.     public static void main(String[] args)
  11.     {
  12.         Scanner console = new Scanner(System.in);
  13.         DecimalFormat df = new DecimalFormat("#.##");
  14.        
  15.         int n = Integer.parseInt(console.nextLine());
  16.        
  17.         double max = 1000000000.0;
  18.         double min = -1000000000.0;
  19.        
  20.         // For odd positions of 'n'
  21.         double oddSum = 0d;
  22.         double oddMin = max;
  23.         double oddMax = min;
  24.        
  25.         // For even positions of 'n'
  26.         double evenSum = 0d;
  27.         double evenMin = max;
  28.         double evenMax = min;
  29.        
  30.         for (int i = 1; i <= n; i++)
  31.         {
  32.             double num = Double.parseDouble(console.nextLine());
  33.            
  34.             if (i % 2 != 0) {
  35.                 if (num < oddMin) oddMin = num;
  36.                 if (num > oddMax) oddMax = num;
  37.                
  38.                 oddSum += num;
  39.             }
  40.             else {
  41.                 if (num < evenMin) evenMin = num;
  42.                 if (num > evenMax) evenMax = num;
  43.                
  44.                 evenSum += num;
  45.             }
  46.         }
  47.        
  48.         System.out.printf("OddSum=%s,%nOddMin=%s,%nOddMax=%s,%n",
  49.                           df.format(oddSum),
  50.                           ((oddMin == max) ? "No" : df.format(oddMin)),
  51.                           ((oddMax == min) ? "No" : df.format(oddMax)));
  52.        
  53.         System.out.printf("EvenSum=%s,%nEvenMin=%s,%nEvenMax=%s%n",
  54.                           df.format(evenSum),
  55.                           ((evenMin == max) ? "No" : df.format(evenMin)),
  56.                           ((evenMax == min) ? "No" : df.format(evenMax)));
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment