Advertisement
deyanmalinov

01. Sum Adjacent Equal Numbers

Feb 27th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.20 KB | None | 0 0
  1. package com.company;
  2. import java.text.DecimalFormat;
  3. import java.util.*;
  4. public class Main {
  5.     private int nums;
  6.  
  7.     public static void main(String[] args) {
  8.             Scanner scan = new Scanner(System.in);
  9.             String [] strArr = scan.nextLine().split(" ");
  10.             ArrayList<Double> intList = new ArrayList<>();
  11.  
  12.             for (String num : strArr) {
  13.             intList.add(Double.parseDouble(num));
  14.  
  15.         }
  16. //          List<Double> intList = Arrays.stream(scan.nextLine().split(" "))
  17. //          .map(Double::parseDouble).collect(Collectors.toList());
  18.  
  19. //            for (int i = 0; i < strArr.length; i++) {
  20. //                double num = Double.parseDouble(strArr[i]);
  21. //                intList.add(num);
  22. //            }
  23.  
  24.             for (int i = 0; i < intList.size()-1; i++) {
  25.  
  26.  
  27.             if (intList.get(i).equals(intList.get(i+1))){
  28.                 intList.set(i, intList.get(i)+(intList.get(i+1)));
  29.                 intList.remove(i+1);
  30.                 i=-1;
  31.             }
  32.  
  33.         }
  34.         DecimalFormat form = new DecimalFormat("#.###");
  35.         for (Double num : intList) {
  36.            
  37.             System.out.print(form.format(num)+" ");
  38.  
  39.         }
  40.  
  41.  
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement