如何使用Golang增加数组值的增量。

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

how to add increments array values with golang

问题

如何在Golang中从上到下添加以下数组:

示例:

输入:

[3, 8, 1]

[3, 2, 5]

输出:

[6, 0, 7]

输入:

[7, 6, 7]

[2, 5, 6]

输出:

[9, 1, 4, 1]

这是我的代码:

func main() {
	size := 3
	elements := make([][]int, size)
	for i := 0; i < size; i++ {
		elements[i] = make([]int, size)
		for j := 0; j < size; j++ {
			fmt.Scanln(&elements[i][j])
		}
	}
	fmt.Println("输入的数组:", elements)
	result := make([]int, size)
	for i := 0; i < size; i++ {
		for j := 0; j < size; j++ {
			result[j] += elements[i][j]
		}
	}
	fmt.Println("相加后的数组:", result)
}

请注意,我只翻译了代码部分,其他内容不会被翻译。

英文:

how to add the following array from top to bottom in golang

example :

input:

[3, 8, 1]

[3, 2, 5]

output:

[6, 0, 7]

input:

[7, 6, 7]

[2, 5, 6]

output:

[9, 1, 4, 1]

here is my code :

func main() {
	size := 3
	elements := make([]int, size)
	for i := 0; i &lt; 3; i++ {
		fmt.Scanln(&amp;elements[i])
	}
	fmt.Println(&quot;2,5,7&quot;, elements)
	result := 0
	for i := 0; i &lt; size; i++ {
		result += elements[i]
	}
	fmt.Println(&quot;Sum of elements of array:&quot;, result)
}

答案1

得分: 1

根据你的问题输入和输出示例,似乎你需要从两个输入数组中取3个元素,并将它们相加。根据你提供的代码片段,很难理解你想要实现什么...但是让我假设你只关心这些输入和输出示例,那么你可以这样做:

package main

import "fmt"

func main() {
  size := 3
  elements1 := make([]int, size)
  elements2 := make([]int, size)
  // 为第一个输入数组 elements1 获取元素
  for i := 0; i < size; i++ {
    fmt.Scanln(&elements1[i])
  }

  // 为第二个输入数组 elements2 获取元素
  for i := 0; i < size; i++ {
    fmt.Scanln(&elements2[i])
  }

  // output 存储我们的输出数组
  output := []int{}
  // 这个变量存储要添加到下一个索引的值,例如 20 + 10 得到 3
  pushToNextIndex := 0

  for i, v := range elements1 {
    sum := v + elements2[i] + pushToNextIndex
    pushToNextIndex = 0

    if sum >= 10 {
      output = append(output, sum%10)
      pushToNextIndex = sum / 10
      continue
    }

    output = append(output, sum)
  }

  // 如果在迭代所有值之后仍然有值,则将其附加为新的数组元素
  if pushToNextIndex > 0 {
    output = append(output, pushToNextIndex)
  }

  fmt.Println(output)
}

如果这不是你想要的,请告诉我!

英文:

From your question's input and output samples it seems like you need to take 3 elements for two input arrays and add them together. It's difficulty to understand what you're trying to achieve by following your code snippets... But lemmy assume you're only concerned with those inputs and output samples, then here is what you can do

package main

import &quot;fmt&quot;

func main() {
  size := 3
  elements1 := make([]int, size)
  elements2 := make([]int, size)
  //take elements for the first input array elements1
  for i := 0; i &lt; size; i++ {
	fmt.Scanln(&amp;elements1[i])
  }

  //take elements for the second input array elements2
  for i := 0; i &lt; size; i++ {
	fmt.Scanln(&amp;elements2[i])
  }

  //output stores our output array
  output := []int{}
  //this store the value to add to the next index eg. 20 + 10 takes 3
  pushToNextIndex := 0

  for i, v := range elements1 {
	sum := v + elements2[i] + pushToNextIndex
	pushToNextIndex = 0

	if sum &gt;= 10 {
		output = append(output, sum%10)
		pushToNextIndex = sum / 10
		continue
	}

	output = append(output, sum)
  }

 //if there is still value after iterating all values then append this as the 
 // new array element
  if pushToNextIndex &gt; 0 {
	output = append(output, pushToNextIndex)
  }

  fmt.Println(output)
 }

Please lemmy know if this is not what you're looking for!

huangapple
  • 本文由 发表于 2022年6月9日 16:50:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/72557371.html
匿名

发表评论

匿名网友

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

确定