如何在Go语言中找到单元格的总和?

huangapple go评论113阅读模式
英文:

How to find the sum of cells in go?

问题

实现SumCells(cells)函数。

输入:一个任意的Cell变量集合cells。

返回:一个单独的Cell变量,对应于对cells中每个单元格中的元素求和。

  1. type Cell [2]float64
  2. func SumCells(cells ...Cell) Cell {
  3. sum := Cell{}
  4. for _, row := range cells { // 遍历行
  5. for cellIndex, cell := range row {
  6. sum[cellIndex] += cell
  7. }
  8. }
  9. return sum
  10. }

有人可以解释一下如何编写这个函数吗?

英文:

Implement the function SumCells(cells).

Input: an arbitrary collection cells of Cell variables.

Return: a single Cell variable corresponding to summing corresponding elements in every cell in cells.
如何在Go语言中找到单元格的总和?

  1. type Cell [2]float64
  2. func SumCells(cells ...Cell) Cell {
  3. sum := 0.0
  4. var c Cell
  5. c = append(c, cells)
  6. for rowIndex, row := range cells { // loop through rows
  7. for cellIndex := range row {
  8. sum += [rowIndex][cellIndex]
  9. }
  10. }
  11. return c
  12. }

Could someone explain how to write this function?

答案1

得分: 1

Cell是一个一维数组或简单数组,在SumCells函数中,Cell或[]Cell需要一个Cells数组[]Cell{{1,3}, {3, 4}},所以你可以简单地这样做Go Playground

  1. func SumCells(cells []Cell) float64 {
  2. var sum float64
  3. for _, cell := range cells {
  4. sum += cell[0] + cell[1]
  5. }
  6. return sum
  7. }
英文:

Cell is a single dimensional array or simple array and in SumCells ...Cell or []Cell want's an array of Cells []Cell{{1,3}, {3, 4}} so you can simply do this Go Playground

  1. func SumCells(cells []Cell) float64 {
  2. var sum float64
  3. for _, cell := range cells {
  4. sum += cell[0] + cell[1]
  5. }
  6. return sum
  7. }

答案2

得分: 0

以下是翻译好的内容:

  1. func SumCells(cells ...Cell) (sum Cell) {
  2. for _, c := range cells {
  3. sum[0] += c[0]
  4. sum[1] += c[1]
  5. }
  6. return sum
  7. }
  8. type Cell [2]float64

或者,如果你想要未来能适应Cell中元素数量的变化:

  1. func SumCells(cells ...Cell) (sum Cell) {
  2. for _, c := range cells {
  3. for i, v := range c {
  4. sum[i] += v
  5. }
  6. }
  7. return sum
  8. }
  9. type Cell [2]float64
英文:

Something like this should do you:

  1. func SumCells(cells ...Cell) (sum Cell) {
  2. for _, c := range cells {
  3. sum[0] += c[0]
  4. sum[1] += c[1]
  5. }
  6. return sum
  7. }
  8. type Cell [2]float64

Or, if you wanted to future-proof yourself in case the number of elements in a Cell changes:

  1. func SumCells(cells ...Cell) (sum Cell) {
  2. for _, c := range cells {
  3. for i, v := range c {
  4. sum[i] += v
  5. }
  6. }
  7. return sum
  8. }
  9. type Cell [2]float64

答案3

得分: 0

  1. // Cell是一个二维点
  2. type Cell [2]float64
  3. // SumCells将一组cell相加
  4. func SumCells(cells ...Cell) Cell {
  5. var sum Cell
  6. for _, cell := range cells {
  7. sum = sum.Add(cell)
  8. }
  9. return sum
  10. }
  11. // Cell.Add将两个cell相加
  12. // 这是Cell类型的方法
  13. func (c Cell) Add(other Cell) Cell {
  14. return Cell{
  15. c[0] + other[0],
  16. c[1] + other[1],
  17. }
  18. }
英文:
  1. // Cell is a 2D point
  2. type Cell [2]float64
  3. // SumCells adds a bunch of cells together
  4. func SumCells(cells ...Cell) Cell {
  5. var sum Cell
  6. for _, cell := range cells {
  7. sum = sum.Add(cell)
  8. }
  9. return sum
  10. }
  11. // Cell.Add adds two cells together
  12. // This is a method on the Cell type
  13. func (c Cell) Add(other Cell) Cell {
  14. return Cell{
  15. c[0] + other[0],
  16. c[1] + other[1],
  17. }
  18. }

huangapple
  • 本文由 发表于 2022年9月11日 01:07:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/73673703.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定