Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 4. Fold and Sum
- Read an array of 4*k integers, fold it like shown below, and print the sum of the upper and lower two rows (each holding 2 * k integers):
- Examples
- Input Output Comments
- 5 2 3 6 7 9 5 6 +
- 2 3 =
- 7 9
- 1 2 3 4 5 6 7 8 5 5 13 13 2 1 8 7 +
- 3 4 5 6 =
- 5 5 13 13
- 4 3 -1 2 5 0 1 9 8 6 7 -2 1 8 4 -1 16 14 -1 3 4 -2 7 6 +
- 2 5 0 1 9 8 =
- 1 8 4 -1 16 14
- Hints
- • Create the first row after folding: the first k numbers reversed, followed by the last k numbers reversed.
- • Create the second row after folding: the middle 2*k numbers.
- • Sum the first and the second rows.
- using System;
- using System.Linq;
- namespace _04FoldAndSum
- {
- class Program
- {
- static void Main(string[] args)
- {/*
- int[] initArr = Console.ReadLine().Split(" ").Select(int.Parse).ToArray();
- int leftFoldIndex = initArr.Length / 4 - 1;
- int rightFoldIndex = 3 * initArr.Length / 4;
- int[] topArr = new int[initArr.Length / 2];
- int howManyNumbersSoFar = 0;
- for (int i = leftFoldIndex; i >= 0; i--)
- {
- howManyNumbersSoFar++;
- topArr[leftFoldIndex - i] = initArr[i];
- }
- for (int i = initArr.Length - 1; i >= rightFoldIndex; i--)
- {
- topArr[initArr.Length - 1 - i + howManyNumbersSoFar] = initArr[i];
- }
- int[] bottomArr = new int[initArr.Length / 2];
- for (int i = leftFoldIndex + 1; i < rightFoldIndex; i++)
- {
- bottomArr[i - howManyNumbersSoFar] = initArr[i];
- }
- for (int i = 0; i < topArr.Length; i++)
- {
- Console.Write(topArr[i] + bottomArr[i] + " ");
- }*/
- int[] inputArr = Console.ReadLine().Split().Select(int.Parse).ToArray();
- int k = inputArr.Length / 4;
- int[] newArr = new int[2 * k];
- for (int i = 0; i < k; i++)
- {
- newArr[i] = inputArr[k - (i + 1)] + inputArr[k + i];
- newArr[newArr.Length - 1 - i] = inputArr[newArr.Length - 1 - i + k] + inputArr[(newArr.Length - 1 - i) + (k + 2 * i + 1)];
- }
- Console.WriteLine(string.Join(" ", newArr));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement