题目:
x=1+x+x^2/2!+x^3/3!+x^4/4!+........
计算前20项和,前提是不能JAVA自带的求POW方法 自己写一个这样的方法
我的程序只执行了一次:我在这里只计算前4项的和·希望能给个算法·
public class test {
static int p;
//static int b;
public static int power(int x,int n)
{ int y=1;
for(int i=1;i<n+1;i++)
y=y*x*x;
b=myexp(y,4);
return b;
}
//public static int myexp(int k,int o)
//{ int h=1;
// int exp=1;
// for(int u=1;u<o+1;u++)
//h=h*u;
// exp=exp+k/h;
// return exp;
// }
public static void main(String[] args) {
p=power(1,4);
System.out.println("p="+p);
}}
x=1+x+x^2/2!+x^3/3!+x^4/4!+........
计算前20项和,前提是不能JAVA自带的求POW方法 自己写一个这样的方法
我的程序只执行了一次:我在这里只计算前4项的和·希望能给个算法·
public class test {
static int p;
//static int b;
public static int power(int x,int n)
{ int y=1;
for(int i=1;i<n+1;i++)
y=y*x*x;
b=myexp(y,4);
return b;
}
//public static int myexp(int k,int o)
//{ int h=1;
// int exp=1;
// for(int u=1;u<o+1;u++)
//h=h*u;
// exp=exp+k/h;
// return exp;
// }
public static void main(String[] args) {
p=power(1,4);
System.out.println("p="+p);
}}
{
if(n==1) return x;
else return x*power(x,n-1);
}
{
if(x==1)return 1;
else return x*exp(x-1);
}
exp()是求x的阶乘~
在MAIN怎么写才能使X变动呀 然后把前N项和加起来