Професионална програма
Loading...
+ Нов въпрос
petaryankov00 avatar petaryankov00 3 Точки

02. Pascal Triangle

Здравейте, имам проблем с решаването на задачата Pascal Triangle от More Excercise в C# Fundamentals. Ако някой може да ми даде някакви насоки как да реша задачата с МАСИВИ. Благодаря ви предварително :)

Ето и условието на задачата:

The triangle may be constructed in the following manner: In row 0 (the topmost row), there is a unique nonzero entry 1. Each entry of each subsequent row is constructed by adding the number above and to the left with the number above and to the right, treating blank entries as 0. For example, the initial number in the first (or any other) row is 1 (the sum of 0 and 1), whereas the numbers 1 and 3 in the third row are added to produce the number 4 in the fourth row.

If you want more info about it: https://en.wikipedia.org/wiki/Pascal's_triangle

Print each row elements separated with whitespace.

Hints

  • The input number n will be 1 <= n <= 60
  • Think about proper type for elements in array
  • Don’t be scary to use more and more arrays

 

1
Fundamentals Module
MartinBG avatar MartinBG 3861 Точки

Ето примерно решение с използването на масиви:

using System;

namespace PascalTriangle
{
    internal static class Program
    {
        private static void Main(string[] args)
        {
            var lines = int.Parse(Console.ReadLine());
            var pascalTriangle = new long[lines][];

            for (var row = 0; row < lines; row++)
            {
                pascalTriangle[row] = new long[row + 1];
                pascalTriangle[row][0] = 1;  // first element is 1
                pascalTriangle[row][^1] = 1; // last element is 1

                for (var col = 1; col < row; col++)
                {
                    pascalTriangle[row][col] = pascalTriangle[row - 1][col - 1] + pascalTriangle[row - 1][col];
                }
            }

            for (var row = 0; row < lines; row++)
            {
                Console.WriteLine(string.Join(" ", pascalTriangle[row]));
            }
        }
    }
}

 

1
AleksandraToncheva avatar AleksandraToncheva 25 Точки

Здравей, надявам се си оправил задачата, но все пак ти пращам моето решение, може да ти е от полза - https://github.com/tonchevaAleksandra/C-SHARP-Fundamentals-Problems/blob/master/FUNDArrayExercises/PascalTriangle/Program.cs

0
yanchev.ilian avatar yanchev.ilian 18 Точки

https://pastebin.com/JHj7aJmp

Тук има две примерни решения 100/100 с назъбен масив (jagged array).

 

Here are two sample solutions giving 100 points, I hope I have been helpful.
0