#include
int power(int x, int p)
{
if(1 == p)
return x;
if(0 == (p % 2))
return power(x * x, p / 2);
else
return power(x * x, p / 2) * x;
}
int polynomial(const int * coefficient, size_t size, int x)
{
int sum = 0;
for(size_t i = size - 1; i != 0; --i)
{
sum = x * sum + coefficient[i];
}
sum = x * sum + coefficient[0];
return sum;
}
int main(int argc, char* argv[])
{
int coeff[] = {1, 2, 3, 4};
std::cout << polynomial(coeff, sizeof(coeff) / sizeof(int), 3) << std::endl;
std::cout << power(5, 5) << std::endl;
return 0;
}
求多项式的值与求幂的快速算法
最新推荐文章于 2023-12-26 14:20:46 发布