Advertisement
social1986

Untitled

Dec 4th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. using System;
  2.  
  3. namespace _4.Pascal_Triangle
  4. {
  5. public class Program
  6. {
  7. public static void Main()
  8. {
  9. var n = long.Parse(Console.ReadLine());
  10. if (n == 1)
  11. {
  12. Console.WriteLine(1);
  13. }
  14. else
  15. {
  16. var pascalTringleMatrix = new long[n][];
  17. pascalTringleMatrix[0] = new long[] { 1 };
  18. pascalTringleMatrix[1] = new long[] { 1, 1 };
  19.  
  20. for (int row = 2; row < pascalTringleMatrix.Length; row++)
  21. {
  22. var currentLength = row + 1;
  23. pascalTringleMatrix[row] = new long[currentLength];
  24.  
  25. pascalTringleMatrix[row][0] = 1;
  26. pascalTringleMatrix[row][currentLength - 1] = 1;
  27.  
  28. for (int col = 1; col < currentLength - 1; col++)
  29. {
  30. var top = pascalTringleMatrix[row - 1][col];
  31. var topLeft = pascalTringleMatrix[row - 1][col - 1];
  32. pascalTringleMatrix[row][col] = top + topLeft;
  33. }
  34. }
  35.  
  36. foreach (var row in pascalTringleMatrix)
  37. {
  38. Console.WriteLine(string.Join(" ", row));
  39. }
  40. }
  41. }
  42. }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement