Advertisement
Guest User

GaussTrick2

a guest
Feb 14th, 2020
1,315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.92 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Scanner;
  4.  
  5. public class GaussTrick {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.         String[] input = scanner.nextLine().split("\\s+");
  10.  
  11.         List<Integer> inputList = new ArrayList<>();
  12.         for (int i = 0; i < input.length; i++) {
  13.             inputList.add(Integer.parseInt(input[i]));
  14.         }
  15.  
  16.         List<Integer> newList = new ArrayList<>();
  17.  
  18.         int i = 0;
  19.         while (i < inputList.size() / 2) {
  20.             int sum = inputList.get(i) + inputList.get(inputList.size() - 1 - i);
  21.             newList.add(sum);
  22.             i++;
  23.         }
  24.         if (inputList.size() % 2 != 0) {
  25.             newList.add(inputList.get(inputList.size() / 2));
  26.         }
  27.  
  28.         for (int j = 0; j < newList.size(); j++) {
  29.             System.out.print(newList.get(j) + " ");
  30.         }
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement