求下列多项式前k项的和,1 - 1/2 + 2/3 - 3/4 +4/5 - 5/6 + ... 要求结果表示为真分式形式Java
时间: 2024-01-22 13:02:56 浏览: 150
```java
public class Main {
public static void main(String[] args) {
double a = 1, b = 2, sum = 0;
double t = a / (b * b);
double n;
int i;
// 请在此处输入n的值
n = 10;
for (i = 1; i <= (n - 1); i++) {
a = a + 1;
b = b + 1;
t = a / (b * b);
sum += t;
}
System.out.println(1 - sum);
}
}
```
相关问题
求下列多项式前k项的和,1 - 1/2 + 2/3 - 3/4 +4/5 - 5/6 + ...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);
}
}
```
用java编程求下列多项式前k项的和,1 - 1/2 + 2/3 - 3/4 +4/5 - 5/6 + ...
以下是使用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);
}
}
```
阅读全文