Advertisement
CR7CR7

Problem 01

Oct 16th, 2022
829
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.17 KB | None | 0 0
  1. import java.text.DecimalFormat;
  2. import java.util.Scanner;
  3. import java.util.Arrays;
  4. import java.util.List;
  5. import java.util.stream.Collectors;
  6.  
  7. public class Main {
  8.     public static void main(String[] args) {
  9.         Scanner scanner = new Scanner(System.in);
  10.  
  11.         List<Double> numbersList= Arrays.stream(scanner.nextLine().split("\\s+"))
  12.                 .map(Double::parseDouble).collect(Collectors.toList());
  13.  
  14.         for (int i = 0; i < numbersList.size()-1; i++) {
  15.             //make two variables
  16.             double currentNum= numbersList.get(i);
  17.             double nextNum =numbersList.get(i+1);
  18.             if(currentNum == nextNum){
  19.                 numbersList.set(i,currentNum + nextNum);
  20.                 numbersList.remove(i+1);
  21.  
  22.             }
  23.         }
  24.         System.out.println(joinItems(numbersList," "));
  25.  
  26.  
  27.     }
  28.    
  29.     public static String joinItems(List<Double>list,String delimeter) {
  30.         DecimalFormat df =new DecimalFormat("0.#");
  31.         String result =" ";
  32.         for (double item:list) {
  33.             String numDf =df.format(item) + delimeter;
  34.             result+=numDf;
  35.            
  36.         }
  37.         return result;
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement