/*
用递归求解多项式的值
p(x)=a(n)*x^n + ... + a(1)*x + a(0);
ary[0]=a(n), .. ,ary[n]=a(0);
*/
float CRecursion::polymonial(const float ary[], const int n, const float x)
{
float y = 0;
if(0 == n)
return ary[n];
else
{
y = polymonial(ary, n-1, x)*x + ary[n];
return y;
}
}
/*
递归求解排列
ary[0~n-1]=1~n,求出这n个数字的所有排列情况
n:ary的长度,k:当前排列的元素个数
*/
void CRecursion::permutation(int ary[],const int k, const int n)
{
CLibrary m_Library;
int i=0;
if(1 == k)
{
for(i = 0; i < n; i++)
cout << ary[i];
cout << endl;
}
else
{
for(i = n-k; i < n; i++)
{
m_Library.swap(ary[i], ary[n-k]);
permutation(ary, k-1, n);
m_Library.swap(ary[i], ary[n-k]);
}
}
}
void main()
{
/
递归求解多项式
/*
const float ary[5] = {2, 3, 1, 3, 2};
float x = 2;
const int n = 5;
CRecursion m_Recursion;
float y = m_Recursion.polymonial(ary, n, x);
cout << "the result is " << y <<endl;
*/
/
递归求解排列
int ary[3] = {1, 2, 3};
CRecursion m_Recursion;
m_Recursion.permutation(ary, 3, 3);
getchar();
return 0;
}