用java语言编写程序S=1+1⼀1+2+1⼀1+2+3+...........+1⼀1+2+3+......+N

2024-12-19 21:22:29
推荐回答(3个)
回答1:

应该是这样的吧
S=1+1/(1+2)+1/(1+2+3)+...........+1/(1+2+3+......+N)

double sum = 0; //和
int up = 1; //分子
int down = 1;//分母
int N = 1;//N的值也就是有多少项
for(int i = 1;i<= N;i++){
double tempSum = (up*1.0)/down;//计算
sum +=tempSum;//和
down +=i+1; //分母增加
}
System.out.println("sum="+sum);

回答2:

public class BaiduDemo3 {
public static void main(String[] a) {
System.out.println("1+1/1+2+1/1+2+3+...........+1/1+2+3+......+100 = "+getResult2(100));
}

public static float getResult2(int n) {
float sum = 0f;
for (int i = 1; i <= n; i++) {
sum += (1 / getReulst(i));
}
return sum;
}

public static float getReulst(int n) {
if (n == 1)return 1.0f;
else return getReulst(n - 1) + n;
}
}

回答3:

public double method2(int n){
if (n==0){
return 1;
}else{
return method2(n-1)+ 1.0/method3(n);
}
}
public int method3(int n){
int sum=0;
for (int i =1;i<=n;i++){
sum+=i;
}
System.out.println(sum);
return sum;
}