求下列多项式前k项的和,1 - 1/2 + 2/3 - 3/4 +4/5 - 5/6 + ...Java解法
时间: 2024-01-08 09:03:37 浏览: 76
广东省广州市天河外国语学校2020-2021学年上学期八年级期中数学【试卷+答案】.docx
```java
public class Main {
public static void main(String[] args) {
double a = 1, b = 2, sum = 0;
double t = a / (b * b);
double n;
Scanner input = new Scanner(System.in);
n = input.nextDouble();
for (int i = 1; i <= (n - 1); i++) {
a = a + 1;
b = b + 1;
t = a / (b * b);
sum += t;
}
System.out.println(1 - sum);
}
}
```
阅读全文