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

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

How to find the sum of cells in go?

问题

实现SumCells(cells)函数。

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

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

type Cell [2]float64

func SumCells(cells ...Cell) Cell {
    sum := Cell{}
    for _, row := range cells { // 遍历行
        for cellIndex, cell := range row {
            sum[cellIndex] += cell
        }
    }
    return sum
}

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

英文:

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语言中找到单元格的总和?

type Cell [2]float64

func SumCells(cells ...Cell) Cell {
    sum := 0.0
    var c Cell
	c = append(c, cells)
    for rowIndex, row := range cells { // loop through rows
        for cellIndex := range row {
        sum += [rowIndex][cellIndex]
        }
    }
    return c
} 

Could someone explain how to write this function?

答案1

得分: 1

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

func SumCells(cells []Cell) float64 {
    var sum float64
    for _, cell := range cells {
        sum += cell[0] + cell[1]
    }
    return sum
}
英文:

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

func SumCells(cells []Cell) float64 {
	var sum float64
	for _, cell := range cells {
		sum += cell[0] + cell[1]
	}
	return sum
}

答案2

得分: 0

以下是翻译好的内容:

func SumCells(cells ...Cell) (sum Cell) {
	for _, c := range cells {
		sum[0] += c[0]
		sum[1] += c[1]
	}
	return sum
}

type Cell [2]float64

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

func SumCells(cells ...Cell) (sum Cell) {
	for _, c := range cells {
		for i, v := range c {
			sum[i] += v
		}
	}
	return sum
}

type Cell [2]float64
英文:

Something like this should do you:

func SumCells(cells ...Cell) (sum Cell) {
	for _, c := range cells {
		sum[0] += c[0]
		sum[1] += c[1]
	}
	return sum
}

type Cell [2]float64

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

func SumCells(cells ...Cell) (sum Cell) {
	for _, c := range cells {
		for i, v := range c {
			sum[i] += v
		}
	}
	return sum
}

type Cell [2]float64

答案3

得分: 0

// Cell是一个二维点
type Cell [2]float64

// SumCells将一组cell相加
func SumCells(cells ...Cell) Cell {
	var sum Cell
	for _, cell := range cells {
		sum = sum.Add(cell)
	}
	return sum
}

// Cell.Add将两个cell相加
// 这是Cell类型的方法
func (c Cell) Add(other Cell) Cell {
	return Cell{
		c[0] + other[0], 
		c[1] + other[1],
	}
}
英文:
// Cell is a 2D point
type Cell [2]float64

// SumCells adds a bunch of cells together
func SumCells(cells ...Cell) Cell {
	var sum Cell
	for _, cell := range cells {
		sum = sum.Add(cell)
	}
	return sum
}

// Cell.Add adds two cells together
// This is a method on the Cell type
func (c Cell) Add(other Cell) Cell {
	return Cell{
		c[0] + other[0], 
		c[1] + other[1],
	}
}

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:

确定