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

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

UTXO選擇策略

UTXO選擇策略

Go
慕娘9325324 2022-06-27 15:01:24
我需要在選擇 UTXO 時應用特定的策略。該策略應盡可能減少 utxo 的使用。應該設置此策略的邊界,理想情況下,應優先考慮較少數量的 utxo,直到 10 倍比率。為了使問題更簡單,我們假設有一個整數列表:= []int{},我需要找到元素'target',其中:list[index] = target,如果這樣的元素不存在,那么我需要從 slice 中找到大于 target 但需要 <= target*10 的第一個元素如果我找不到這樣的元素,那么我需要找到兩個元素 x,y 其中:x + y = target,如果這樣的元素不存在,我需要從 slice 中找到大于 target 但需要的前兩個元素<= 目標*10如果我無法找到這樣的元素,那么我需要找到三個元素 x、y、z 其中:x + y + z = 目標,如果這樣的元素不存在,我需要從切片中找到大于目標的前三個元素但需要 <= target*10如果我找不到這樣的三個元素,我需要找到四個、五個……直到 len(list)。示例 1:target = 6list := []int {1,2, 6, 10}result = list[2] = 6示例 2:target = 6list := []int {1,2, 3, 10}result = list[3] = 10示例 3:target = 6list := []int {1,2, 3, 10}result = list[3] = 10示例 4:target = 6list := []int {1,3, 3, 61}result = list[1]  + list[2]= 6請參閱下面的測試用例,我需要通過遞歸或以某種方式改進以獲得通用解決方案:func Test_SelectUtxo(t *testing.T){    x := 6    list := []int{1, 2, 3, 64, 65, 62, 62, 62, 61, 59}        fmt.Println("ONE = x")    for i := 0; i < len(list) - 1; i ++ {        if list[i] == x {            fmt.Println(i)            break        }    }    fmt.Println("ONE <= x*10")    for i := 0; i < len(list); i ++ {        if list[i] > x {            if list[i] <= x*10 && list[i] > x {                fmt.Println(list[i])                break            }        }    }    fmt.Println("TWO = x")    out:    for i := 0; i < len(list) - 1; i ++ {        for j:=i + 1; j < len(list); j ++ {            if list[i] + list[j] == x {                fmt.Println(i)                fmt.Println(j)                break out            }        }    }    fmt.Println()    fmt.Println("TWO <= x*10")    out1:    for i := 0; i < len(list) - 1; i ++ {        for j:=i + 1; j < len(list); j ++ {            if list[i] + list[j] <= x*10 && list[i] + list[j] > x {                fmt.Println(i)                fmt.Println(j)                break out1            }        }    }
查看完整描述

1 回答

?
慕村225694

TA貢獻1880條經驗 獲得超4個贊

一種解決方案:

  1. size = 1

  2. 使用遞歸(函數名稱= getCombination在下面的代碼片段中)來獲取size輸入數組中元素的所有組合。

  3. 檢查每個組合是否滿足0 -> i的要求,如果是,則返回(完成

  4. 如果沒有任何組合匹配,則size++,然后轉到步驟 2。

片段:

import (

  "fmt"

)

var combination = []int{}

func GetCombination(src []int,size int, offset int) [][]int { // get all combinations for **size** elements in the elements of src array

  result := [][]int{}

  if size == 0 {

    temp := make([]int, len(combination))

    copy(temp, combination)

    return append(result, temp)

  }

  for i:=offset; i<=len(src) - size; i++ {

    combination = append(combination, src[i])

    temp := GetCombination(src, size-1, i+1)

    result = append(result, temp...)

    combination = combination[:len(combination)-1]

  }

  return result[:]

}

func sum(items []int) int {

  total := 0

  for _, v := range items {

    total += v

  }

  return total

}

func GetBestPair(items []int, target int) []int {

    for i := 1; i < len(items)+1; i++ {

        result := GetCombination(items, i, 0) // get all possible combinations for 1 -> len(items) elements of Array=items

        // fmt.Println("Combinations for ", i, " elements:", result)

        for j := 0; j < len(result); j++ {

            total := sum(result[j])

            if total < target {

                continue

            }

            if total == target {

                return result[j]

            }

            if total < target*10 {

                return result[j]

            }

        }

    }

    return []int{}

}


func main () {

  fmt.Println("Result", GetBestPair([]int{1, 3, 3, 61}, 6))

}

上述測試用例的輸出


Combinations for  1  elements: [[1] [3] [3] [61]]

Combinations for  2  elements: [[1 3] [1 3] [1 61] [3 3] [3 61] [3 61]]

Result: [3 3]


查看完整回答
反對 回復 2022-06-27
  • 1 回答
  • 0 關注
  • 148 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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