Advertisement
JulianJulianov

14. Fold and Sum

Feb 10th, 2020
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. 4. Fold and Sum
  2. 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):
  3.  
  4. Examples
  5. Input Output Comments
  6. 5 2 3 6 7 9 5 6 +
  7. 2 3 =
  8. 7 9
  9. 1 2 3 4 5 6 7 8 5 5 13 13 2 1 8 7 +
  10. 3 4 5 6 =
  11. 5 5 13 13
  12. 4 3 -1 2 5 0 1 9 8 6 7 -2 1 8 4 -1 16 14 -1 3 4 -2 7 6 +
  13. 2 5 0 1 9 8 =
  14. 1 8 4 -1 16 14
  15. Hints
  16. • Create the first row after folding: the first k numbers reversed, followed by the last k numbers reversed.
  17. • Create the second row after folding: the middle 2*k numbers.
  18. • Sum the first and the second rows.
  19.  
  20. using System;
  21. using System.Linq;
  22.  
  23. namespace _04FoldAndSum
  24. {
  25. class Program
  26. {
  27. static void Main(string[] args)
  28. {/*
  29. int[] initArr = Console.ReadLine().Split(" ").Select(int.Parse).ToArray();
  30.  
  31. int leftFoldIndex = initArr.Length / 4 - 1;
  32. int rightFoldIndex = 3 * initArr.Length / 4;
  33.  
  34. int[] topArr = new int[initArr.Length / 2];
  35.  
  36. int howManyNumbersSoFar = 0;
  37. for (int i = leftFoldIndex; i >= 0; i--)
  38. {
  39. howManyNumbersSoFar++;
  40. topArr[leftFoldIndex - i] = initArr[i];
  41. }
  42. for (int i = initArr.Length - 1; i >= rightFoldIndex; i--)
  43. {
  44. topArr[initArr.Length - 1 - i + howManyNumbersSoFar] = initArr[i];
  45. }
  46.  
  47.  
  48. int[] bottomArr = new int[initArr.Length / 2];
  49.  
  50. for (int i = leftFoldIndex + 1; i < rightFoldIndex; i++)
  51. {
  52. bottomArr[i - howManyNumbersSoFar] = initArr[i];
  53. }
  54.  
  55.  
  56. for (int i = 0; i < topArr.Length; i++)
  57. {
  58. Console.Write(topArr[i] + bottomArr[i] + " ");
  59. }*/
  60.  
  61. int[] inputArr = Console.ReadLine().Split().Select(int.Parse).ToArray();
  62.  
  63. int k = inputArr.Length / 4;
  64. int[] newArr = new int[2 * k];
  65.  
  66. for (int i = 0; i < k; i++)
  67. {
  68. newArr[i] = inputArr[k - (i + 1)] + inputArr[k + i];
  69. newArr[newArr.Length - 1 - i] = inputArr[newArr.Length - 1 - i + k] + inputArr[(newArr.Length - 1 - i) + (k + 2 * i + 1)];
  70. }
  71. Console.WriteLine(string.Join(" ", newArr));
  72. }
  73. }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement