-
Notifications
You must be signed in to change notification settings - Fork 3
/
turtle-noturtle.html
158 lines (153 loc) · 4.14 KB
/
turtle-noturtle.html
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Turtle First</title>
<style>
html {
font-family: 'Courier New', Courier, monospace;
}
table.gridtable {
font-family: verdana, arial, sans-serif;
font-size: 11px;
color: #333333;
border-width: 1px;
border-color: #666666;
border-collapse: collapse;
}
table.gridtable td {
text-align: left;
width: 10px;
border-width: 1px;
padding: 2px;
border-style: solid;
border-color: #666666;
background-color: #ffffff;
}
</style>
</head>
<body>
<script type="text/javascript">
// 累加累乘斐波那契九九表n皇后
function sum() {
let n = 0
for (let i = 1; i <= 50000; i++) {
n += i
}
document.write(`累加到50000: ${n}<br>`)
}
function mul() {
let n = 1n
for (let i = 2n; i <= 50n; i++) {
n *= i
}
document.write(`累乘到50: ${n}<br>`)
}
function fibonacci(n) {
if (n === 0) {
return 0
} else if (n === 1) {
return 1
} else {
return fibonacci(n - 1) + fibonacci(n - 2)
}
}
function fibNoCache() {
let d = +new Date()
document.write(
'硬算Fib(40): ' + fibonacci(40) + ',' + (+new Date() - d) + '<br>'
)
}
function fibonacciMemoized(n, memo = {}) {
if (n in memo) {
return memo[n]
}
if (n === 0n) {
return 0n
} else if (n === 1n) {
return 1n
}
memo[n] =
fibonacciMemoized(n - 1n, memo) + fibonacciMemoized(n - 2n, memo)
return memo[n]
}
function fibMemo() {
let d = +new Date()
document.write(
'缓存算Fib(50n): ' +
fibonacciMemoized(50n) +
',' +
(+new Date() - d) +
'<br>'
)
}
function nineTable() {
document.write('<br>')
document.write('九九表<br>')
for (let i = 1; i <= 9; i++) {
for (let k = i; k <= 9; k++) {
let val = k * i
document.write(
`${i}x${k} = ${val < 10 ? ' ' + val : val} `
)
}
document.write('<br>')
}
}
function solveNQueens(size = 8) {
const solutions = []
function backtrack(row, cols = 0, d1 = 0, d2 = 0, board = []) {
// 如果已经处理完最后一行,即找到了一种解决方案,将其加入 solutions 数组
if (row === size) {
solutions.push(board.map(cols => cols.join('</td><td>')))
return
}
for (let col = 0, mask; col < size; col++) {
mask = 1 << col // 创建当前列的位掩码
if (
!(cols & mask) &&
!(d1 & (1 << (row + col))) &&
!(d2 & (1 << (row - col + size - 1)))
) {
board.push(
Array(size)
.fill('.')
.fill('Q', col, col + 1)
) // 将皇后放置在棋盘上
backtrack(
row + 1,
cols | mask,
d1 | (1 << (row + col)),
d2 | (1 << (row - col + size - 1)),
board
)
board.pop()
}
}
}
backtrack(0)
return solutions
}
function printNqueens() {
const size = 6
document.write(`${size}皇后问题<br>`)
solveNQueens(size).map((x, idx) => {
document.write(`第${idx + 1}种解法<br>`)
document.write(
`<table id=t${idx} class="gridtable"><tr><td>${x.join(
'</td></tr><tr><td>'
)}</td></tr></table>`
)
document.querySelector(`#t${idx}`).style.width = size * 18 + 'px'
document.write('<br>')
})
}
sum()
mul()
fibNoCache()
fibMemo()
nineTable()
printNqueens()
</script>
</body>
</html>