英文:
Question about sorting 2 dimensional array in Golang
问题
我对Go语言中的二维数组有一个问题。我不知道为什么我使用的排序方法不起作用,但是在Java、C和C++中它运行得很好,但是当我在Go语言中应用相同的排序方法时,我不确定出了什么问题,有时它给我正确的结果,有时候它根本不排序。请帮忙看看。非常感谢。
package main
import (
"fmt"
)
var employee int = 0
var day int = 1
func main() {
list := [][]int{{2, 4, 3, 4, 5, 8, 8},
{7, 3, 4, 3, 3, 4, 4},
{3, 3, 4, 3, 3, 2, 2},
{9, 3, 4, 7, 3, 4, 1},
{3, 5, 4, 3, 6, 3, 8},
{3, 4, 4, 6, 3, 4, 4},
{3, 7, 4, 8, 3, 8, 4},
{6, 3, 5, 9, 2, 7, 9}}
var result [8][2]int
for i := 0; i < len(result); i++ {
var total int = 0
for j := 0; j < len(list[i]); j++ {
total += list[i][j]
}
result[i][employee] = i
result[i][day] = total
}
sort(result)
fmt.Println("The decreasing order is:")
for i := 0; i < len(result); i++ {
fmt.Printf("Employee %v's total dayoff is %v\n", result[i][employee], result[i][day])
}
}
func sort(list [8][2]int) {
for i := 0; i < len(list); i++ {
var max_day int = list[i][day]
var max_employee int = list[i][employee]
var max_index int = i
for j := i + 1; j < len(list); j++ {
if list[j][day] > max_day {
max_day = list[j][day]
max_employee = list[j][employee]
max_index = j
}
}
if max_index != i {
list[max_index][employee] = list[i][employee]
list[max_index][day] = list[i][day]
list[i][employee] = max_employee
list[i][day] = max_day
}
}
}
英文:
I have a question about 2 dimensional array in GoLang. I do not know why the sort method I use does not work, but It works well in java, C, and C++, but I am not sure what is wrong when I applied the same sort method in Golang, sometimes it give me the right result, sometimes, it does not sort at all. Please help. Thank you so much in advance.
package main
import (
"fmt"
)
var employee int = 0
var day int = 1
func main() {
list := [][] int {{2, 4, 3, 4, 5, 8, 8},
{7, 3, 4, 3, 3, 4, 4},
{3, 3, 4, 3, 3, 2, 2},
{9, 3, 4, 7, 3, 4, 1},
{3, 5, 4, 3, 6, 3, 8},
{3, 4, 4, 6, 3, 4, 4},
{3, 7, 4, 8, 3, 8, 4},
{6, 3, 5, 9, 2, 7, 9}}
var result [8][2] int
for i := 0; i < len(result); i++ {
var total int = 0
for j := 0; j < len(list[i]); j++ {
total += list[i][j]
}
result[i][employee] = i
result[i][day] = total
}
sort(result)
fmt.Println("The decreasing order is:")
for i := 0; i < len(result); i++ {
fmt.Printf("Employee %v's total dayoff is %v\n", result[i][employee], result[i][day])
}
}
func sort(list[8][2] int) {
for i := 0; i < len(list); i++ {
var max_day int = list[i][day]
var max_employee int = list[i][employee]
var max_index int = i
for j := i + 1; j < len(list); j++ {
if list[j][day] > max_day {
max_day = list[j][day]
max_employee = list[j][employee]
max_index = j
}
}
if max_index != i {
list[max_index][employee] = list[i][employee]
list[max_index][day] = list[i][day]
list[i][employee] = max_employee
list[i][day] = max_day
}
}
}
答案1
得分: 3
你正在修改 sort()
函数中的 list
的副本,请将其原型更改为:
func sort(list *[8][2]int) {
并调用它:
sort(&result)
英文:
You're modifying a copy of list
in your sort()
function, change its prototype to:
func sort(list *[8][2]int) {
and call it:
sort(&result)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论