import java.util.Scanner;
/*
设多项式 exp(x)=1+x+x^2/2!+……+x^i/i!,|x^i/i!|<=10^-10
输入 x
输出 exp(x)的值(保留小数点后四位)
*/
class 算法_计算多项式 {
public static void main(String[] args) {
System.out.print("请输入待计算多项式的X的值(保留两位小数):");
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
double t=1.0,sum=1.0;
for(int i=1; ;i++){
t = t*x/i;
sum += t;
if(Math.abs(t)<=1e-10)
break;
}
System.out.println(String.format("%.2f",sum));
}
}
/*
import java.text.DecimalFormat;
import java.util.Scanner;
public class Main2{
static int i=0;//初始值
static double s=1.0;
static double t=1.0;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
double x = in.nextDouble();
i = i+1;
t = t*(x/i);
s = s+t;
while(Math.abs(t) > Math.pow(10, -10)){
i = i+1;
t = t*(x/i);
s = s+t;
}
//取四位小数
DecimalFormat df =new java.text.DecimalFormat("#.0000");
System.out.println(df.format(s));
}
}
*/
计算多项式 :设多项式 exp(x)=1+x+x^2/2!+……+x^i/i!,|x^i/i!|<=10^-10 输入 x 输出 exp(x)的值(保留小数点后四位)
于 2020-08-19 20:57:01 首次发布