大家好。你可以创建一个集合,你可以像这样填充它
func main() {
m := make(map[int]bool, 0)
for i := 0; i < 20; i++ {
m[i] = true
}
log.Println(m)
}
是这样吗,如果是这样,如何为集合创建一个子集
需要这样的东西
множество[подмножество1{член1, член2, член3...}, подмножество2{член1, член2, член3 ...}...etc]
package main
import (
"math"
"sync"
)
type Set struct {
sync.Mutex
mp map[*SubSet]bool
}
type SubSet struct {
sync.Mutex
mp map[float64]bool
}
func NewSet() *Set {
return &Set{
mp: map[*SubSet]bool{},
}
}
func NewSubSet() *SubSet {
return &SubSet{
mp: map[float64]bool{},
}
}
func (s *SubSet) InsertInSubset(key float64) *SubSet {
s.Lock()
defer s.Unlock()
s.mp[key] = true
return s
}
func (s *Set) InsertInSet(key *SubSet, value bool) *Set {
s.Lock()
defer s.Unlock()
s.mp[key] = true
return s
}
func CelcionSet(celcions []float64) {
celcions = QS(celcions)
lencelcions := len(celcions)
set := NewSet()
for i := 0; i < lencelcions; i++ {
rng := math.Trunc(celcions[i]/10) * 10
sub := NewSubSet()
set.InsertInSet(sub.InsertInSubset(celcions[i]), true)//это конечно же не сработает, но не понимаю как заставить работать чтобы в множество вставлялось подмножество
}
}
func QS(nums []float64) []float64 {
// find len of arr
lennums := len(nums)
// if arr contains less then 2 items, nothing to sort
if lennums < 2 {
return nums
}
toehold := nums[0] //or nums[lennums/2]
// items which less than toehold
left := make([]float64, 0)
// items which more than toehold
right := make([]float64, 0)
for _, num := range nums[1:] {
// move to right if item more than toehold
if num > toehold {
right = append(right, num)
} else {
// move to left if item less than toehold
left = append(left, num)
}
}
// recoursive sorting left side
nums = append(QS(left), toehold)
// recoursive sorting right side
nums = append(nums, QS(right)...)
return nums
}
1 个回答