add new solutions

This commit is contained in:
kanna5 2026-01-27 18:38:43 +09:00
parent 67cad91898
commit eb6ffe8114
No known key found for this signature in database
24 changed files with 933 additions and 14 deletions

View file

@ -0,0 +1,53 @@
package q1116
type void struct{}
type ZeroEvenOdd struct {
n int
c0, c1, c2 chan void
}
func NewZeroEvenOdd(n int) *ZeroEvenOdd {
zeo := &ZeroEvenOdd{
n: n,
c0: make(chan void, 1),
c1: make(chan void, 1),
c2: make(chan void, 1),
}
zeo.c0 <- void{}
return zeo
}
func (z *ZeroEvenOdd) Zero(printNumber func(int)) {
for i := range z.n {
<-z.c0
printNumber(0)
if (i+1)%2 == 0 {
z.c2 <- void{}
} else {
z.c1 <- void{}
}
}
}
func (z *ZeroEvenOdd) Even(printNumber func(int)) {
for i := range z.n {
if (i+1)%2 != 0 {
continue
}
<-z.c2
printNumber(i + 1)
z.c0 <- void{}
}
}
func (z *ZeroEvenOdd) Odd(printNumber func(int)) {
for i := range z.n {
if (i+1)%2 == 0 {
continue
}
<-z.c1
printNumber(i + 1)
z.c0 <- void{}
}
}