sum=1-1/2!+1/3!-1/4!+…(-1)n-1*1/n用java
时间: 2023-11-23 16:06:47 浏览: 151
以下是Java实现sum=1-1/2!+1/3!-1/4!+…(-1)n-1*1/n的代码:
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
double sum = 1, x = -1, f = 1;
for (int i = 2; i <= n; i++) {
f *= i;
sum += x / f;
x = -x;
}
System.out.println("sum=" + sum);
}
}
```
阅读全文