用java编程求下列多项式前k项的和,1 - 1/2 + 2/3 - 3/4 +4/5 - 5/6 + ...
时间: 2024-01-07 14:04:15 浏览: 92
以下是使用Java编程求解多项式前k项的和的示例代码:
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int k = scanner.nextInt();
double sum = 0;
int sign = 1;
for (int i = 1; i <= k; i++) {
sum += sign * i / (i + 1.0);
sign = -sign;
}
System.out.printf("%.4f", sum);
}
}
```
阅读全文