第3章 栈和队列
3.3 栈和队列的应用
综合应用题 第3题
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#define MaxSize 100
using namespace std;
typedef char ElemType;
double p(int n,double x)
{
struct stack{
int no;
double val;
}St[MaxSize];
int top=-1;
double f1=1,f2=2*x;
for(int i=n;i>=2;i--)
{
top++;
St[top].no=i;
}
while(top>=0)
{
St[top].val=2*x*f2-2*(St[top].no-1)*f1;
f1=f2;
f2=St[top].val;
top--;
}
return f2;
}
int main() {
int n,x;
cout<<"请输入n,x:"<<endl;
cin>>n>>x;
if(n==1)
cout<<0;
else
cout<<p(n,x);
return 0;
}