3
0

More than 5 years have passed since last update.

# 日能研の問題をGo言語で力づくで解いてみた(2018/11)

Last updated at Posted at 2018-11-16

2018年11月の日能研の広告の問題を力づくで解くページを見かけた

ので，私は Go 言語で解いてみる。

``````package main

import "fmt"

func Permutations(cards []int) <-chan []int {
ch := make(chan []int)
go func() {
defer close(ch)
perm(ch, make([]int, 0, len(cards)), cards)
}()
return ch
}
func dup(list []int) []int {
l := make([]int, len(list), cap(list))
copy(l, list)
return l
}
func perm(ch chan<- []int, list []int, rest []int) {
if len(rest) == 0 {
ch <- dup(list)
return
}
for i, v := range rest {
restx := dup(rest)
restx = append(restx[:i], restx[i+1:]...)
listx := append(list, v)
perm(ch, listx, restx)
}

}

func list2num(list []int) int {
v := 0
for i, t := len(list)-1, 1; i >= 0; i, t = i-1, t*10 {
v += list[i] * t
}
return v
}
func main() {
for p := range Permutations([]int{1, 2, 3, 4, 5, 6}) {
res := true
for i := 2; i <= 6; i++ {
if list2num(p[:i])%i != 0 {
res = false
break
}
}
if res {
fmt.Println(list2num(p))
}
}
}
``````

これで問題なく動作する

あとは順列を生成するのに goroutine でジェネレータ・パターンを構成していることだろうか。

## 参考

3
0
0

Register as a new user and use Qiita more conveniently

1. You get articles that match your needs
2. You can efficiently read back useful information
3. You can use dark theme
What you can do with signing up
3
0