E. 2 канала

Верно решивших: 62
Рейтинг быстрых решений
Очень похожие решения учитываются в рейтинге один раз.
1 2 3 4 5
Время отправки: 2020-05-31 11:53:59 ID: 142 Время: 0.052s
package main import "sync" func Merge2Channels(f func(int) int, in1 <-chan int, in2 <-chan int, out chan<- int, n int) { ch1 := make(chan int, 100) ch2 := make(chan int, 100) go readChannel(in1, ch1, n) go readChannel(in2, ch2, n) prev := make(chan interface{}, 2) prev = nil for ; 0 < n; n-- { next := make(chan interface{}, 2) go func(prev <-chan interface{}, next chan<- interface{}) { var r1, r2 int if prev != nil { <-prev } x, y := <-ch1, <-ch2 next <- struct{}{} wg := &sync.WaitGroup{} wg.Add(1) go func() { r1 = f(x) wg.Done() }() r2 = f(y) wg.Wait() res := r1 + r2 if prev != nil { <-prev } out <- res next <- struct{}{} }(prev, next) prev = next } } func readChannel(in <-chan int, out chan<- int, n int) { for ; n > 0; n-- { out <- <-in } }
Время отправки: 2020-05-31 18:48:44 ID: 288 Время: 0.052s
package main func Merge2Channels(f func(int) int, in1 <-chan int, in2 <-chan int, out chan<- int, n int) { go func() { temp_chan1 := make(chan chan int, n) temp_chan2 := make(chan chan int, n) go func() { for i := 0; i < n; i++ { res_1 := <-<-temp_chan1 res_2 := <-<-temp_chan2 out <- res_1 + res_2 } }() input := func(in <-chan int, temp_chan chan chan int) { for i := 0; i < n; i++ { buffer := make(chan int) temp_chan <- buffer value := <-in go func(buffer chan int, value int) { buffer <- f(value) }(buffer, value) } } go input(in1, temp_chan1) go input(in2, temp_chan2) }() }
Время отправки: 2020-06-02 09:49:43 ID: 368 Время: 0.052s
package main func calc(f func(int) int, x int, out chan<- int, prev <-chan struct{}, next chan<- struct{}) { y := f(x) <-prev out <- y close(next) } func calcChannel(f func(int) int, in <-chan int, out chan<- int, n int) { prev := make(chan struct{}) first := prev for i := 0; i < n; i++ { next := make(chan struct{}) x := <-in go calc(f, x, out, prev, next) prev = next } close(first) } func Merge2Channels(f func(int) int, in1 <-chan int, in2 <-chan int, out chan<- int, n int) { go func() { out1 := make(chan int) out2 := make(chan int) go calcChannel(f, in1, out1, n) go calcChannel(f, in2, out2, n) for i := 0; i < n; i++ { x1 := <-out1 x2 := <-out2 y := x1 + x2 out <- y } }() }
Время отправки: 2020-06-02 13:28:13 ID: 376 Время: 0.052s
package main import "sync" func Merge2Channels(f func(int) int, in1 <-chan int, in2 <-chan int, out chan<- int, n int) { go func() { resultSum := make([]int, n) wg := &sync.WaitGroup{} wg.Add(n) for i := 0; i < n; i++ { x1 := <-in1 x2 := <-in2 go func(i int) { defer wg.Done() res1 := make(chan int) res2 := make(chan int) go wrapF(f,res1, x1) go wrapF(f,res2, x2) resultSum[i] = <-res1 + <-res2 }(i) } wg.Wait() for i := 0; i < n; i++ { out<- resultSum[i] } }() } func wrapF(f func(int) int, out chan<- int,num int){ out <- f(num) }
Время отправки: 2020-06-07 14:28:36 ID: 416 Время: 0.052s
package main import ( "sync" ) func main() { } func Merge2Channels(f func(int) int, in1 <-chan int, in2 <-chan int, out chan<- int, n int) { go func() { resultSum := make([]int, n) wg := &sync.WaitGroup{} wg.Add(n) for i:=0; i < n; i++ { x1 := <- in1 x2 := <- in2 go func(i int) { defer wg.Done() f1 := make(chan int) f2 := make(chan int) go func(x int){ f1 <- f(x1) }(x2) go func(x int){ f2 <- f(x2) }(x2) resultSum[i] = <-f1 + <-f2 }(i) } wg.Wait() for i:=0; i<n; i++ { out <- resultSum[i] } }() }
Время отправки: 2020-05-31 19:09:50 ID: 296 Время: 0.053s
package main import ( "runtime" "sync" ) type Result struct { R map[[2]int]int sync.Mutex } var result = Result{} type Order struct { NN [][2]int sync.Mutex } var order = Order{} type Carret struct { K int sync.Mutex } var K = Carret{} var mutex sync.Mutex var wg = &sync.WaitGroup{} func Merge2Channels(f func(int) int, in1 <-chan int, in2 <-chan int, out chan<- int, n int) { order.NN = make([][2]int, 0) result.R = make(map[[2]int]int) K.K = 0 process := func(n int, fch chan int) { fch <- f(n) } go func(f func(int) int, in1 <-chan int, in2 <-chan int, out chan<- int, n int) { wg.Add(n) i := 0 for i < n { mutex.Lock() n1 := <-in1 runtime.Gosched() n2 := <-in2 order.NN = append(order.NN, [2]int{n1, n2}) go func(n1 int, n2 int) { var f1 = make(chan int) var f2 = make(chan int) go process(n1, f1) go process(n2, f2) r := <-f1 + <-f2 result.Lock() result.R[[2]int{n1, n2}] = r wg.Done() result.Unlock() }(n1, n2) mutex.Unlock() i++ } wg.Wait() for K.K < len(result.R) { K.Lock() out <- result.R[order.NN[K.K]] K.K++ K.Unlock() } }(f, in1, in2, out, n) }
Время отправки: 2020-06-02 10:21:21 ID: 369 Время: 0.053s
package main func Merge2Channels(f func(int) int, in1 <-chan int, in2 <-chan int, out chan<- int, n int) { go func() { fx := make([]chan int, n) for i := 0; i < n; i++ { fx[i] = make(chan int, 2) go func(ch chan int, x int) { ch <- f(x) }(fx[i], <-in1) go func(ch chan int, x int) { ch <- f(x) }(fx[i], <-in2) } for i := 0; i < n; i++ { out <- (<-fx[i] + <-fx[i]) } }() }
Время отправки: 2020-05-31 14:05:27 ID: 244 Время: 0.053s
package main import( "sync" ) func Merge2Channels(f func(int) int, in1 <-chan int, in2 <-chan int, out chan<- int, n int) { go func(f func(int) int, in1 <-chan int, in2 <-chan int, out chan<- int, n int) { process := func(n int, fch chan int) { fch <- f(n) } var order = make([][2]int, 0, n) var results = make(map[[2]int]int) var wg = &sync.WaitGroup{} wg.Add(n) i := 0 for i < n { var f1 = make(chan int) var f2 = make(chan int) n1 := <-in1 go process(n1, f1) n2 := <-in2 go process(n2, f2) order = append(order, [2]int{n1, n2}) go func(n1 int, n2 int, f1 chan int, f2 chan int) { results[[2]int{n1, n2}] = (<-f1) + (<-f2) wg.Done() }(n1, n2, f1, f2) i++ } wg.Wait() for _, key := range order { out <- results[key] } }(f, in1, in2, out, n) }
Время отправки: 2020-05-31 12:35:21 ID: 195 Время: 0.053s
package main func Merge2Channels(f func(int) int, in1 <-chan int, in2 <-chan int, out chan<- int, n int){ procIn := func(cin chan chan int, in <-chan int, n int) { defer close(cin) for i := 0; i < n; i++ { x, ok := <-in if !ok { return } c := make(chan int) cin <- c go func(c chan int) { c <- f(x) close(c) }(c) } } cin1 := make(chan chan int, n) cin2 := make(chan chan int, n) go procIn(cin1, in1, n) go procIn(cin2, in2, n) go func() { for i := range cin1 { out <- (<-i + <-<-cin2) } }() }
Время отправки: 2020-06-05 20:01:27 ID: 407 Время: 0.053s
package main import ( "sync" ) func Merge2Channels(f func(int) int, in1 <-chan int, in2 <- chan int, out chan<- int, n int) { channel1, channel2 := make([]int, n), make([]int, n) var wg1, wg2 sync.WaitGroup go func() { for i := 0; i < n; i++ { wg1.Add(1) x := <-in1 go func(num int, idx int) { channel1[idx] = f(num) defer wg1.Done() }(x, i) } }() go func() { for i := 0; i < n; i++ { wg2.Add(1) x := <-in2 go func(num int, idx int) { channel2[idx] = f(num) defer wg2.Done() }(x, i) } }() go func() { wg1.Wait() wg2.Wait() for i := 0; i < n; i++ { out <- channel1[i] + channel2[i] } }() }
1 2 3 4 5