-
Notifications
You must be signed in to change notification settings - Fork 0
/
combination.js
133 lines (121 loc) · 3.42 KB
/
combination.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
const max_cost = 1e8
function main() {
let n = Number(document.getElementById("n").value)
let k = Number(document.getElementById("k").value)
var filters = []
let repeating = document.getElementById("repeating").checked
let grouping = document.getElementById("grouping").checked
if (document.getElementById("no_zero_begin").checked) {
filters.push(zeroBeginFilter)
}
if (document.getElementById("use_filter").checked) {
try {
filters.push(eval(document.getElementById('filter').value))
} catch (e) {
alert(e.message)
return
}
}
let computer = new Computer(n, k, repeating, grouping, filters)
try {
computer.execute()
document.getElementById("area").value = computer.allResult()
document.getElementById("result").value = computer.resultNumber()
} catch (error) {
alert(error)
}
}
class Computer {
constructor (n, k, repeating, grouping, filters) {
this.n = n
this.k = k
this.repeating = repeating
this.grouping = grouping
this.filters = filters
this.result = []
}
checkOverflow() {
let result
if (this.grouping) {
// 組合
if (this.repeating) result = factorial(this.n + this.k - 1) / (factorial(this.k) * factorial(this.n - 1))
else result = factorial(this.n, this.k) / factorial(this.k)
} else {
// 重複排列
if (this.repeating) result = Math.pow(this.n, this.k)
else result = factorial(this.n, this.k)
}
console.log('時間複雜度:', result)
if (result > max_cost) {
throw Error(`${result} 超過運算上限: ${max_cost}`)
}
}
execute() {
this.checkOverflow()
this.exhaustive(0, [])
}
exhaustive(counter, existing) {
let begin = 0
if (this.grouping && existing.length > 0) {
begin = existing[existing.length - 1]
}
let end = this.n
if (!this.repeating) {
end -= counter
}
for (let i = begin; i < end; i++) {
let child = existing.slice()
child.push(i)
if (counter < this.k-1) {
this.exhaustive(counter + 1, child)
} else {
if (!this.repeating) {
child = this.transform(child)
}
if (this.filter(child)) {
this.result.push(child)
}
}
}
}
transform(array) {
let result = []
let values = Array.from(Array(this.n).keys())
for (let i of array) {
result.push(values[i])
values.splice(i, 1)
}
return result
}
filter(array) {
for (let f of this.filters) {
if (!f(array)) {
return false
}
}
return true
}
allResult() {
let result = []
for (let row of this.result) {
result.push(row.join(""))
}
return result.join(" ")
}
resultNumber() {
return this.result.length
}
}
function factorial(n, k=n) {
let result = 1
for (let i = 0; i < k; i++) {
result *= n - i
}
return result
}
function repeatingFilter(array) {
return (new Set(array)).size === array.length;
}
function zeroBeginFilter(array) {
return array[0] != 0
}