英文:
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 < 3; i++ {
fmt.Scanln(&elements[i])
}
fmt.Println("2,5,7", elements)
result := 0
for i := 0; i < size; i++ {
result += elements[i]
}
fmt.Println("Sum of elements of array:", 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 "fmt"
func main() {
size := 3
elements1 := make([]int, size)
elements2 := make([]int, size)
//take elements for the first input array elements1
for i := 0; i < size; i++ {
fmt.Scanln(&elements1[i])
}
//take elements for the second input array elements2
for i := 0; i < size; i++ {
fmt.Scanln(&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 >= 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 > 0 {
output = append(output, pushToNextIndex)
}
fmt.Println(output)
}
Please lemmy know if this is not what you're looking for!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论