Advertisement
veronikaaa86

02. Gauss' Trick

Jun 7th, 2023
939
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.96 KB | None | 0 0
  1. package list;
  2.  
  3. import java.text.DecimalFormat;
  4. import java.util.Arrays;
  5. import java.util.List;
  6. import java.util.Scanner;
  7. import java.util.stream.Collectors;
  8.  
  9. public class P02GaussTrick {
  10.     public static void main(String[] args) {
  11.         Scanner scanner = new Scanner(System.in);
  12.  
  13.         String input = scanner.nextLine();
  14.  
  15.         List<Integer> numbersList = Arrays
  16.                 .stream(input.split(" "))
  17.                 .map(Integer::parseInt)
  18.                 .collect(Collectors.toList());
  19.  
  20.         int size = numbersList.size();
  21.         for (int i = 0; i < size / 2; i++) {
  22.             int firstNum = numbersList.get(i);
  23.             int secondNum = numbersList.get(numbersList.size() - 1);
  24.  
  25.             int sum = firstNum + secondNum;
  26.             numbersList.set(i, sum);
  27.             numbersList.remove(numbersList.size() - 1);
  28.         }
  29.  
  30.         for (int item : numbersList) {
  31.             System.out.print(item + " ");
  32.         }
  33.     }
  34. }
  35.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement