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 關注
- 138 瀏覽
添加回答
舉報