欧拉数特指 Eulerian Number,不同于 Euler numbers,Euler's number 哦。
组合数学中,欧拉数(Eulerian Number)是从1到n中正好满足m个元素大于前一个元素(具有m个“上升”的排列)条件的排列个数。定义为: ?
计算公式:
?相关推到:
计算结果:
?
?源程序:
using System;
namespace Legalsoft.Truffer.Algorithm
{
public static partial class Number_Sequence
{
/// <summary>
/// 欧拉数(递归)算法
/// </summary>
/// <param name="n"></param>
/// <param name="m"></param>
/// <returns></returns>
public static int Eulerian_Number(int n, int m)
{
if (m >= n || n == 0)
{
return 0;
}
if (m == 0)
{
return 1;
}
int a = (n - m) * Eulerian_Number(n - 1, m - 1);
int b = (m + 1) * Eulerian_Number(n - 1, m);
return (a + b);
}
/// <summary>
/// 欧拉数(非递归)算法
/// </summary>
/// <param name="n"></param>
/// <param name="m"></param>
/// <returns></returns>
public static int Eulerian_Number_Second(int n, int m)
{
int[,] dp = new int[n + 1, m + 1];
for (int i = 1; i <= n; i++)
{
for (int j = 0; j <= m; j++)
{
if (i > j)
{
if (j == 0)
{
dp[i, j] = 1;
}
else
{
dp[i, j] = ((i - j) * dp[i - 1, j - 1]) + ((j + 1) * dp[i - 1, j]);
}
}
}
}
return dp[n, m];
}
}
}
——————————————————————
POWER? BY? TRUFFER.CN
|