-
Notifications
You must be signed in to change notification settings - Fork 429
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8595009
commit 4947d34
Showing
2 changed files
with
31 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |