# java中的系列总和

2023-01-05 16:51:25

int num=5,n=4,i,j,res=0,sum=1;

int k=num+1;

for(i=1;i<=num-1;i++)

{

k=k-1;

sum=1;

for(j=1;j<=n;j++)

{

sum=sum*k;

k=k+1;

}

}

res=res+sum;

System.out.println(res);

## 3 回答

TA贡献1575条经验 获得超7个赞

int num = 5;

int sum = 0;

int n = 4;

for(int i = 0; i<n ;i++) {

int ele = 1;

for (int k = num + i; k < num + i + n - 1; k++) {

ele *= k;

}

sum += ele;

}

System.out.println(sum);

TA贡献0条经验 获得超1个赞

import static java.util.stream.IntStream.range;

...

int num = 5, n = 4;

int sum1 = range(0, n).map(l -> range(num + l, num + l + 3).reduce(1, (a, r) -> a * r)).sum();

System.out.println(sum1);

1770

range(0, n) // ensures that the sum operation is executed `n` times.

.map(l -> range(num + l, num + l + 3) // generates sequences of 3 numbers starting from `num` increased by `l`

.reduce(1, (a, r) -> a * r) // calculates the product of the sequences of 3 numbers

.sum() // sums the product of all three numbers

TA贡献1550条经验 获得超12个赞

int num=5,n=4,i,j,res=0,sum=1;

int k=num+1;

for(i=1;i<=num-1;i++)

{

k=k-1;

sum=1;

for(j=1;j<=n;j++)

{

sum=sum*k;

k=k+1;

}

}

res=res+sum;   // This line should be *inside* the previous curly bracket.

System.out.println(res);

res = res + sum;

sum += k * (k + 1) * (k + 2);

sum += ... 意思是一样的sum = sum +  ...

k++; 意思是一样的 k = k + 1;

k--; 意思是一样的 k = k - 1;

A < B + 1（1个减法）

A <= B（2 个减法加上一个逻辑或）。

int num = 5,

n = 4,

k = n,

i = k,

res = 0;

for(i = k; i < k + n; i++) // Gives n terms, e.g. k = {num, num + 1, ..., num + n - 1}

{

res += k * (k + 1) * (k + 2);

k++;

}

System.out.println(res);

• 3 回答
• 0 关注
• 11 浏览

0/150