亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

對稀疏 CSC 矩陣 Golang 的行進行排序

對稀疏 CSC 矩陣 Golang 的行進行排序

Go
長風秋雁 2023-03-07 10:55:06
我正在嘗試分析稀疏矩陣。面臨按原始矩陣中元素的升序對行進行排序的任務。但我不明白如何在不損壞空元素的情況下做到這一點。我試圖將 sum 數組的元素綁定到行并以某種方式移動它們。但一些元素已從 CSC 結構中刪除??赡苄枰约焊?li/lj 數組,但我對此沒有足夠的數學知識。更準確地說,我不明白如何跟蹤何時應該重新排列元素,除非在結構中明確指定了額外的元素(零)。package mainimport (    "fmt")type CSC struct {    a, lj, li []int}func getEl(i, j int, el *CSC) int {    for k := el.lj[j]; k < el.lj[j+1]; k++ {        if el.li[k] == i {            return el.a[k]        }    }    return 0}func maxSliceEl(lj []int) int {    max := 0    for _, v := range lj {        if v > max {            max = v        }    }    return max}func main() {    ma := CSC{        a:  []int{8, 2, 5, 7, 1, 9, 2},        li: []int{0, 0, 1, 4, 4, 6, 4},        lj: []int{0, 1, 1, 4, 6, 7},    }    n := len(ma.lj) + 1    m := maxSliceEl(ma.li) - 1    fmt.Printf("Col: %v, Row: %v\n", n, m)    maxStr := []int{}    fmt.Println("Initial matrix:")    for i := 0; i < n; i++ {        sumStrEl := 0        for j := 0; j < m; j++ {            fmt.Print(getEl(i, j, &ma), " ")            sumStrEl += getEl(i, j, &ma)        }        maxStr = append(maxStr, sumStrEl)        fmt.Println("|sumStrEl: ", sumStrEl)    }}
查看完整描述

1 回答

?
慕容3067478

TA貢獻1773條經驗 獲得超3個贊

我通過將結構作為解決方案找到了解決問題的方法:元素之和+它們的索引。解決方案比想象中的要簡單,只是缺乏解決稀疏矩陣的實踐。sum 的位置 [i] 必須作為第一個參數傳遞給 getEl 函數。


package main


import (

    "fmt"

    "sort"

)


// Creating a CSC (CCS) matrix structure

type CSC struct {

    // Array of values, column indexes, row indexing

    a, lj, li []int

}


// Getting access to the element

func getEl(i, j int, el *CSC) int {

    for k := el.lj[j]; k < el.lj[j+1]; k++ {

        // If the element string is equal to the string of the searched element, then the element is found

        if el.li[k] == i {

            return el.a[k]

        }

    }

    // Otherwise, we will return 0. It will be entered into the matrix

    return 0

}


func maxSliceEl(lj []int) int {

    max := 0


    for _, v := range lj {

        if v > max {

            max = v

        }

    }

    return max

}


type strInfo struct {

    summa int

    pos   int

}


func main() {

    // Set the CSC matrix

    ma := CSC{

        a:  []int{8, 2, 5, 7, 1, 9, 2},

        li: []int{0, 0, 1, 4, 4, 6, 4},

        lj: []int{0, 1, 1, 4, 6, 7},

    }


    // Define the number of columns

    n := len(ma.lj) + 1

    // Define the number of rows

    m := maxSliceEl(ma.li) - 1

    fmt.Printf("Cols: %v, Rows: %v\n", m, n)


    // Set a variable with a structure type for calculating 

    // the amount in a row and indexing each element in it

    var stringsInfo []strInfo


    fmt.Println("Initial matrix:")

    for i := 0; i < n; i++ {

        sumStrEl := 0


        for j := 0; j < m; j++ {

            sumStrEl += getEl(i, j, &ma)

            fmt.Print(getEl(i, j, &ma), " ")

        }

        fmt.Println("|", sumStrEl)


        // Adding a cell with the sum and index to the slice

        var strI strInfo

        strI.summa = sumStrEl

        strI.pos = i

        stringsInfo = append(stringsInfo, strI)

    }


    fmt.Println("stringsInfo: ", stringsInfo)


    // Sorting the stringsInfo slice in ascending order of the sum elements

    sort.Slice(stringsInfo, func(i, j int) (less bool) {

        return stringsInfo[i].summa < stringsInfo[j].summa

    })


    fmt.Println("stringsInfo: ", stringsInfo)


    fmt.Println("Sorted matrix:")

    for i := range stringsInfo {

        for j := 0; j < m; j++ {

            // Output the matrix by idnex stringsInfo[i].pos

            fmt.Print(getEl(stringsInfo[i].pos, j, &ma), " ")

        }

        fmt.Println("|", stringsInfo[i].summa)

    }

}


查看完整回答
反對 回復 2023-03-07
  • 1 回答
  • 0 關注
  • 118 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號