diff --git a/04-algrithm/06-rectangle/__test__/index.spec.js b/04-algrithm/06-rectangle/__test__/index.spec.js new file mode 100644 index 0000000..900b5eb --- /dev/null +++ b/04-algrithm/06-rectangle/__test__/index.spec.js @@ -0,0 +1,6 @@ +const { check } = require('../index') +describe('长方形中取正方形', () => { + it('', () => { + expect(check(1000, 20)).toBe(26882) + }) +}) \ No newline at end of file diff --git a/04-algrithm/06-rectangle/index.js b/04-algrithm/06-rectangle/index.js new file mode 100644 index 0000000..c70e7e8 --- /dev/null +++ b/04-algrithm/06-rectangle/index.js @@ -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 +} \ No newline at end of file