求下列多项式前k项的和,1 - 1/2 + 2/3 - 3/4 +4/5 - 5/6 + ...Java解法
时间: 2024-01-08 22:03:37 浏览: 80
利用JAVA,求多项式的和
```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);
}
}
```
阅读全文