daily pastebin goal
41%
SHARE
TWEET

4. Pascal Triangle

fight90 Jan 29th, 2018 (edited) 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace SoftUniNetCore
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             /*
  11.           The triangle may be constructed in the following manner: In row 0 (the topmost row),
  12.           there is a unique nonzero entry 1. Each entry of each subsequent row is constructed by
  13.           adding the number above and to the left with the number above and to the right, treating blank
  14.           entries as 0. For example, the initial number in the first (or any other) row is 1 (the sum of 0 and 1),
  15.           whereas the numbers 1 and 3 in the third row are added to produce the number 4 in the fourth row.
  16.             */
  17.  
  18.             int countOfRows = int.Parse(Console.ReadLine());
  19.  
  20.             long[][] pascalTriangle = new long[countOfRows][];
  21.             pascalTriangle[0] = new long[1];
  22.             pascalTriangle[0][0] = 1;
  23.          
  24.  
  25.             for (int i = 1; i < countOfRows; i++)
  26.             {
  27.                 pascalTriangle[i] = new long[i + 1];
  28.                 pascalTriangle[i][0] = 1;
  29.                 pascalTriangle[i][pascalTriangle[i].Length-1] = 1;
  30.                 for (int a = 1; a < pascalTriangle[i].Length-1; a++)
  31.                 {
  32.                     pascalTriangle[i][a] = (pascalTriangle[i - 1][a - 1] + pascalTriangle[i - 1][a]);
  33.                 }
  34.  
  35.  
  36.             }
  37.  
  38.             for (int i = 0; i < pascalTriangle.Length; i++)
  39.             {
  40.                 for (int a = 0; a < pascalTriangle[i].Length; a++)
  41.                 {
  42.                     Console.Write(pascalTriangle[i][a] + " ");
  43.                 }
  44.                 Console.WriteLine();
  45.             }
  46.  
  47.         }
  48.     }
  49. }
RAW Paste Data
Pastebin PRO WINTER Special!
Get 40% OFF Pastebin PRO accounts!
Top