Skip to content

Commit

Permalink
feat: add rectangle
Browse files Browse the repository at this point in the history
  • Loading branch information
su37josephxia committed Jul 27, 2023
1 parent 8595009 commit 4947d34
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
6 changes: 6 additions & 0 deletions 04-algrithm/06-rectangle/__test__/index.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const { check } = require('../index')
describe('长方形中取正方形', () => {
it('', () => {
expect(check(1000, 20)).toBe(26882)
})
})
25 changes: 25 additions & 0 deletions 04-algrithm/06-rectangle/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* 长方形截取正方形
* @param {*} W 长 + 宽 总长度
* @param {*} N 正好能截取的正方形
*/
exports.check = function (W, N) {
function cut(w, h) {
if (w == h) return 1
if (w > h) {
var temp = w; w = h; h = temp
}
var r = h % w
var result = Math.floor(h / w)
if (r > 0) result += cut(w, r)
return result
}

var cnt = 0
for (var i = 1; i <= W; i++) {
for (var j = i; j <= W; j++) {
if (cut(i, j) == N) cnt++
}
}
return cnt
}

0 comments on commit 4947d34

Please sign in to comment.