恩廷格尔组合数(Entringer Number)组合数学的序列数字之一。
E(n,k)是{1,2,…,n+1}的排列数,从k+1开始,先下降后上升。
计算结果:
源代码:
1 文本格式
using System;
namespace Legalsoft.Truffer.Algorithm
{
/// <summary>
/// Entringer Number
/// Entringer数E(n,k)是{1,2,…,n+1}的排列数,从k+1开始,先下降后上升。
/// </summary>
public static partial class Number_Sequence
{
/// <summary>
/// 恩廷格尔组合数的原始(递归)算法
/// </summary>
/// <param name="n"></param>
/// <param name="k"></param>
/// <returns></returns>
public static int Entringer_Number(int n, int k)
{
if (n == 0 && k == 0)
{
return 1;
}
if (k == 0)
{
return 0;
}
return Entringer_Number(n, k - 1) + Entringer_Number(n - 1, n - k);
}
/// <summary>
/// 恩廷格尔组合数的改进(非递归)算法
/// </summary>
/// <param name="n"></param>
/// <param name="k"></param>
/// <returns></returns>
public static int Entringer_Number_Second(int n, int k)
{
int[,] dump = new int[n + 1, k + 1];
dump[0, 0] = 1;
for (int i = 1; i <= n; i++)
{
dump[i, 0] = 0;
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= Math.Min(i, k); j++)
{
dump[i, j] = dump[i, j - 1] + dump[i - 1, i - j];
}
}
return dump[n, k];
}
}
}
——————————————————————
POWER BY TRUFFER.CN
2 代码格式
using System;
namespace Legalsoft.Truffer.Algorithm
{
/// <summary>
/// Entringer Number
/// Entringer数E(n,k)是{1,2,…,n+1}的排列数,从k+1开始,先下降后上升。
/// </summary>
public static partial class Number_Sequence
{
/// <summary>
/// 恩廷格尔组合数的原始(递归)算法
/// </summary>
/// <param name="n"></param>
/// <param name="k"></param>
/// <returns></returns>
public static int Entringer_Number(int n, int k)
{
if (n == 0 && k == 0)
{
return 1;
}
if (k == 0)
{
return 0;
}
return Entringer_Number(n, k - 1) + Entringer_Number(n - 1, n - k);
}
/// <summary>
/// 恩廷格尔组合数的改进(非递归)算法
/// </summary>
/// <param name="n"></param>
/// <param name="k"></param>
/// <returns></returns>
public static int Entringer_Number_Second(int n, int k)
{
int[,] dump = new int[n + 1, k + 1];
dump[0, 0] = 1;
for (int i = 1; i <= n; i++)
{
dump[i, 0] = 0;
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= Math.Min(i, k); j++)
{
dump[i, j] = dump[i, j - 1] + dump[i - 1, i - j];
}
}
return dump[n, k];
}
}
}