递归计算多项式
[cpp]#include <iostream>
#include <stack>
int power(int x, int n)
{
if (n == 0)
return 1;
else
{
int res = 1;
int base = x;
while (n)
{
if (n & 1)
res *= base;
base *= base;
n >>= 1;
}
return res;
}
};
www.zzzyk.com
int fn(int x, int n, int a[])
{
if (n == 0)
return a[0];
else
return fn(x, n - 1, a) + a[n] * power(x, n);
};
int main(int argc,char *argv[])
{
int a[3] = {1, 2, 4};
int res = fn(2, 2, a);
return 0;
}
补充:软件开发 , C++ ,