求1/1!-1/3!+1/5!-1/7!+......+(-1)^n+1/(2n-1)!
分为两个步骤:
1、求阶乘:求出每个单数的阶乘
3、求倒数再相加或相减求和,得出结果(加减是按顺序替换的)
package Exercise;
import java.util.Scanner;
public class two {
public static void main(String[] args) {
//求1/1!-1/3!+1/5!-1/7!+......+(-1)^n+1/(2n-1)!
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
double s = 0.0;
double t = 1;
for (int i = 1; i <= 2*n-1; i+=2) {
s+=t/i;
t=-t;
}
System.out.printf("结果为:%5f",s);
}
}
10
结果为:0.760460
Process finished with exit code 0