Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Lab7CondenseArrayToNumber {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String input = scanner.nextLine();
- String[] inputNumbers = input.split(" ");
- int[] arrayIntegers = new int[inputNumbers.length];
- for (int i = 0; i < arrayIntegers.length; i++) {
- arrayIntegers[i]= Integer.parseInt(inputNumbers[i]);
- }
- while (arrayIntegers.length>1){
- int[] arr = new int[arrayIntegers.length-1];
- for (int i = 0; i < arrayIntegers.length-1 ; i++) {
- arr[i]= arrayIntegers[i] + arrayIntegers[i+1];
- }
- arrayIntegers=arr;
- }
- System.out.println(arrayIntegers[0]);
- }
- }
- /*
- * Write a program to read an array of integers and condense them by summing adjacent couples
- * of elements until a single integer is obtained. For example, if we have 3 elements {2, 10, 3},
- * we sum the first two and the second two elements and obtain {2+10, 10+3} = {12, 13}, then we sum again
- * all adjacent elements and obtain {12+13} = {25}.
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement