How can I obtain the maximum and sum of the second column array in Ruby?

2026-04-11 16:391阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计286个文字,预计阅读时间需要2分钟。

How can I obtain the maximum and sum of the second column array in Ruby?

对于类似以下数组 +s+%3D+%5B%5B1%2C2%5D%2C%5B4%2C6%5D%2C%5B2%2C7%5D%5D,如何在每行的第二列中选取最大值并计算总和,步骤如下:

1. 初始化总和 sum 和最大值 max 为 0。

2.遍历数组 s 的每一行,将当前行的第二列的值赋给 sum。

3.如果当前行的第二列的值大于 max,则更新 max。

4.最后,输出 max 和 sum 的值。

代码如下:

python

s=[[1, 2], [4, 6], [2, 7]]

max_value=0total_sum=0

for a, b in s: total_sum +=b if b > max_value: max_value=b

print(fmax={max_value}, sum={total_sum})

对于像这样的数组

How can I obtain the maximum and sum of the second column array in Ruby?

s = [[1,2],[4,6],[2,7]]

我如何在一个语句中的每一行中选择第二列的最大值和总和

max = 7
sum = 15

我知道

sum = 0 max = 0 s.each{ |a,b| sum+=b;if max<b then max = b end }

会工作.

s.map {| E | e [1]}.max给你最大值

s.map {| E | e [1]}.reduce(:)给你总和.

标签:最大值

本文共计286个文字,预计阅读时间需要2分钟。

How can I obtain the maximum and sum of the second column array in Ruby?

对于类似以下数组 +s+%3D+%5B%5B1%2C2%5D%2C%5B4%2C6%5D%2C%5B2%2C7%5D%5D,如何在每行的第二列中选取最大值并计算总和,步骤如下:

1. 初始化总和 sum 和最大值 max 为 0。

2.遍历数组 s 的每一行,将当前行的第二列的值赋给 sum。

3.如果当前行的第二列的值大于 max,则更新 max。

4.最后,输出 max 和 sum 的值。

代码如下:

python

s=[[1, 2], [4, 6], [2, 7]]

max_value=0total_sum=0

for a, b in s: total_sum +=b if b > max_value: max_value=b

print(fmax={max_value}, sum={total_sum})

对于像这样的数组

How can I obtain the maximum and sum of the second column array in Ruby?

s = [[1,2],[4,6],[2,7]]

我如何在一个语句中的每一行中选择第二列的最大值和总和

max = 7
sum = 15

我知道

sum = 0 max = 0 s.each{ |a,b| sum+=b;if max<b then max = b end }

会工作.

s.map {| E | e [1]}.max给你最大值

s.map {| E | e [1]}.reduce(:)给你总和.

标签:最大值