Pyramid.cs

CSharp · Console

/Program.cs

using System;
					
public class Program
{
	public static int GetCount(int prevFloor, int curCount)
	{
		// закончились блоки для строительства нового уровня (curCount == 0)
		// значит фиксируем текущий построенный уровень (+1 к кол-ву уровней)
		if (curCount == 0) { return 1; }

		int count = 0;
		for (int floor = 1; floor < prevFloor; ++floor)
		{
			if (curCount - floor >= 0)
			{
				count += GetCount(floor, curCount - floor);
			}
		}
		return count;
	}

	public static int GetCountMain(int n)
	{
		int count = 0;

		// проходим по 1-му уровню и рекурсивно находим 
		// кол-во для след.уровня i с кол-вом блоков (n - i)
		for (int i = 1; i <= n; i++)
		{   
			count += GetCount(i, n - i);
		}
		return count;
	}	
	
	public static void Main()
	{
		Console.WriteLine("UniLecs");
		Console.WriteLine(string.Format("Answer = {0}", GetCountMain(3))); // 2
		Console.WriteLine(string.Format("Answer = {0}", GetCountMain(5))); // 3
		Console.WriteLine(string.Format("Answer = {0}", GetCountMain(6))); // 4		
	}
}
;