Advertisement
Guest User

Odd / Even Position

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