Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Text;
- namespace Pascal_Triangle
- {
- class PascalTriangle
- {
- static int IndexValue(int n)
- {
- int result;
- int a = 1;
- int b = n;
- if ((n % 2) == 0)
- {
- result = (a + b) * n / 2;
- }
- else
- {
- result = ((n - 1) / 2) * (a + (b - 1)) + b;
- }
- return result;
- }
- static void Main(string[] args)
- {
- int n = int.Parse(Console.ReadLine());
- int arrSize = 2;
- if (n > 1)
- {
- arrSize = IndexValue(n);
- }
- ulong[] arr = new ulong[arrSize];
- arr[0] = 1;
- int indexSource;
- int indexTarget;
- for (int row = 1; row <= n - 1; row++)
- {
- int k = row + 1;
- indexSource = IndexValue(row - 1);
- indexTarget = IndexValue(k - 1);
- for (int col = 0; col <= row - 1; col++)
- {
- arr[indexTarget + col] += arr[indexSource + col];
- arr[indexTarget + col + 1] += arr[indexSource + col];
- }
- }
- StringBuilder sb = new StringBuilder();
- for (int row = 1; row <= n; row++)
- {
- indexTarget = IndexValue(row - 1);
- sb.Clear();
- for (int col = 0; col <= row - 1; col++)
- {
- sb.Append($"{arr[indexTarget + col]} ");
- }
- Console.WriteLine(sb.ToString().TrimEnd());
- }
- }
- }
- }
Advertisement
RAW Paste Data
Copied
Advertisement