fv1 = P0(x) fv2 = P1(x)
struct Stack{int n; double PnX; int top;}st;
for (int i = n; i >= 2; i--) {top++; st[top].n = i;}
while (top >= 0){st[top].PnX = 2 * x * fv2 - 2 * (st[top].n - 1) * fv1;//这样就完成了对栈的右边那一栏的赋值,赋值完之后要更新fv1和fv2的值 fv1 = fv2; fv2 = st[top].PnX; top--;//最后让top指针从上往下移动}
完整代码附上
// // Created by 黎圣 on 2023/8/2. // #include "iostream" #define MAX 10 struct Stack { int top; int n; double PnX; }st[MAX]; //题目主代码 double ans(int n, double x) { int top = -1; double fv1 = 1; double fv2 = 2 * x; //初始化左边部分 for (int i = n; i >= 2; i--) { top++; st[top].n = i; } //初始化右边部分 while (top >= 0) { st[top].PnX = 2 * x * fv2 - 2 * (st[top].n - 1) * fv1; fv1 = fv2; fv2 = st[top].PnX; top--; } if (n == 0) return fv1; else return fv2; } int main() { double ans1 = ans(0, 1.9); printf("%lf", ans1); return 0; }
这题听起来可能有点乱,要自己推一遍才能清楚为什么fv1和fv2要这么换,也建议各位做题的时候拿上草稿纸括弧也可能是我比较菜… 最后感谢b站up主@吸血小金鱼