這里有點奇怪。我的問題是,人們運行我的代碼會得到和我一樣的結果嗎?如果你這樣做了,是我的代碼有問題(我通常是一個 python 程序員),還是 golang 中的錯誤?系統信息:圍棋版本(1.1.2)的Linux的x64(Fedora的19)代碼的背景信息:我正在做的是找到從三角形頂部到底部的最高成本路線,這是來自 project_euler 18 和 67該bug:我設置了一個名為pathA變量,這是一個整數列表,加上新價值新的int從三角形如3,7,2追加8中應該等于3,2,7,8,但它確實!......直到我設置pathB。pathB被設置正確但是突然pathA是相同的值pathB。tl;dr當我設置另一個變量時,一個變量被覆蓋我的代碼如下:package mainimport ( "fmt")func extendPaths(triangle, prePaths [][]int) [][]int { nextLine := triangle[len(prePaths)] fmt.Println("#####PrePaths: ", prePaths) fmt.Println("#####nextLine: ", nextLine) postPaths := [][]int{{}} for i := 0; i < len(prePaths); i++ { route := prePaths[i] nextA := nextLine[i] nextB := nextLine[i+1] fmt.Println("Next A:", nextA, "Next B:", nextB, "\n") pathA := append(route, nextA) fmt.Println("pathA check#1:", pathA) pathB := append(route, nextB) fmt.Println("pathA check#2:", pathA, "\n") postPaths = append(postPaths, pathA) postPaths = append(postPaths, pathB) } postPaths = postPaths[1:] prePaths = [][]int{postPaths[0]} for i := 1; i < len(postPaths)-1; i += 2 { if getSum(postPaths[i]) > getSum(postPaths[i+1]) { prePaths = append(prePaths, postPaths[i]) } else { prePaths = append(prePaths, postPaths[i+1]) } } prePaths = append(prePaths, postPaths[len(postPaths)-1]) return prePaths}func getSum(sumList []int) int { total := 0 for i := 0; i < len(sumList); i++ { total += sumList[i] } return total}func getPaths(triangle [][]int) { prePaths := [][]int{{triangle[0][0]}} for i := 0; i < len(triangle)-1; i++ { prePaths = extendPaths(triangle, prePaths) }}func main() { triangle := [][]int{{3}, {7, 4}, {2, 4, 6}, {8, 5, 9, 3}} getPaths(triangle)}
Go 變量被覆蓋(錯誤?)
慕婉清6462132
2021-07-02 14:00:44