From 4947d340f74549e2de1fe39c2d7981f11dd253da Mon Sep 17 00:00:00 2001 From: josephxia <15906475@qq.com> Date: Thu, 27 Jul 2023 11:24:16 +0800 Subject: [PATCH] feat: add rectangle --- .../06-rectangle/__test__/index.spec.js | 6 +++++ 04-algrithm/06-rectangle/index.js | 25 +++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 04-algrithm/06-rectangle/__test__/index.spec.js create mode 100644 04-algrithm/06-rectangle/index.js 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