int a[10];
int n=10,x;
cin >> x;
int sum=0;
//一般算法
for (int i = 0; i < n;i++)
{
sum += a[i] * pow(x, i);
}
sum = 0;
//秦九韶算法
for (int i = n-1; i >=0; i--)
{
sum += sum*x + a[n - 1];
}
普通算法运行时间为 n(n+1)/2次乘法+N次加法
秦九韶算法:n次乘法和n次加法
(求 a0*x^0+a1*x^1+a2*x^2+.....+an*x^n)
2求最大子列
int a[10];
int n = 10;
int sum = 0;
int thissum = 0;
//没优化,n^3
for (int i = 0; i < n;i++)
{
for (int j = i; j < n; j++)
{
thissum = 0;
for (int k = i; k < j; k++)
{
thissum += a[k];
}
if (thissum>sum)
{
sum = thissum;
}
}
}
//一次优化之后,O(n)=n^2
for (int i = 0; i < n; i++)
{
thissum = 0;
for (int j = i; j < n; j++)
{
thissum += a[j];
if (thissum>sum)
{
sum = thissum;
}
}
}
//第三次优化,分治法
断电,明天更