1 回答
TA贡献1744条经验 获得超4个赞
从你的问题的输入和输出样本来看,你似乎需要为两个输入数组取 3 个元素并将它们相加。很难通过代码片段来理解你想要实现的目标......但是假设你只关心那些输入和输出样本,那么这就是你可以做的
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)
}
请 lemmy 知道这是否不是您要找的!
- 1 回答
- 0 关注
- 101 浏览
添加回答
举报