diff --git a/04-algrithm/02-finger-guess/__tests__/index.spec.js b/04-algrithm/02-finger-guess/__tests__/index.spec.js new file mode 100644 index 0000000..bc18875 --- /dev/null +++ b/04-algrithm/02-finger-guess/__tests__/index.spec.js @@ -0,0 +1,20 @@ +const { check } = require('../index'); +/*少数服从多数的方式常在人们意见不一致时使用。该方式简单明了,所以除了政界,学校和公司也常 +结果 +当当一定数量的人猜拳时,一次就能定胜负的人数组合方式有多少种呢?以4人为例,表1.3列出了所有可能的组合方式,一共有 12 种。 +*/ +describe('猜拳少数服从多数', () => { + describe('递归解法', () => { + it('3人 9种情况', () => { + expect(check(3)).toBe(9); + }) + + it('4人 12种情况', () => { + expect(check(4)).toBe(12); + }) + + it('100人 5100种情况', () => { + expect(check(100)).toBe(5100); + }) + }) +}) \ No newline at end of file diff --git a/04-algrithm/02-finger-guess/index.js b/04-algrithm/02-finger-guess/index.js new file mode 100644 index 0000000..086dff0 --- /dev/null +++ b/04-algrithm/02-finger-guess/index.js @@ -0,0 +1,24 @@ +// finger guess +exports.check = function check(n) { + var cnt = 0; + for (var rock = 0; rock <= n; rock++) { + for (var paper = 0; paper <= n - rock; paper++) { + var scissors = n - rock - paper; + if (rock > scissors) { + if (rock != paper) { + cnt++; + } + } else if (rock < scissors) { + if (paper != scissors) { + cnt++; + } + } else { + // 相等的情况 + if (paper > rock) { + cnt++; + } + } + } + } + return cnt; +} \ No newline at end of file diff --git a/04-algrithm/03-light/__tests__/index.spec.js b/04-algrithm/03-light/__tests__/index.spec.js new file mode 100644 index 0000000..dc0b784 --- /dev/null +++ b/04-algrithm/03-light/__tests__/index.spec.js @@ -0,0 +1,11 @@ +const { getNum } = require('../index') +// 24小时制 +describe('电子钟的亮灯数', () => { + test('27处亮灯 8800种', () => { + expect(getNum(27)).toBe(8800) + }) + + test('30处亮灯 8360种', () => { + expect(getNum(30)).toBe(8360) + }) +}) \ No newline at end of file diff --git a/04-algrithm/03-light/index.js b/04-algrithm/03-light/index.js new file mode 100644 index 0000000..72d3fcb --- /dev/null +++ b/04-algrithm/03-light/index.js @@ -0,0 +1,26 @@ +exports.getNum = function (N) { + /** + * 获取两位数的亮灯数 + */ + function check(num) { + var light = [6, 2, 5, 5, 4, 5, 6, 3, 7, 6] + return light[Math.floor(num / 10)] + light[num % 10] + } + + var lights = new Array(60) + for (var i = 0; i < 60; i++) { + lights[i] = check(i) + } + + var cnt = 0 + for (var h = 0; h < 24; h++) { + for (var m = 0; m < 60; m++) { + for (var s = 0; s < 60; s++) { + if (lights[h] + lights[m] + lights[s] === N) { + cnt++ + } + } + } + } + return cnt +} \ No newline at end of file diff --git a/04-algrithm/04-fibonacci/__tests__/index.spec.js b/04-algrithm/04-fibonacci/__tests__/index.spec.js new file mode 100644 index 0000000..7ce7ae1 --- /dev/null +++ b/04-algrithm/04-fibonacci/__tests__/index.spec.js @@ -0,0 +1,23 @@ +const { fi, fi2 } = require('../index') + +describe('斐波那契数列', () => { + describe('递归', () => { + it('fibonacci(9) => 34', () => { + expect(fi(9)).toBe(55) + }) + it('fibonacci(16) => 987', () => { + expect(fi(40)).toBe(165580141) + }) + }) + + describe('动态规划', () => { + it('fibonacci(9) => 34', () => { + expect(fi2(9)).toBe(55) + }) + it('fibonacci(16) => 987', () => { + expect(fi2(40)).toBe(165580141) + }) + }) + + +}) \ No newline at end of file diff --git a/04-algrithm/04-fibonacci/index.js b/04-algrithm/04-fibonacci/index.js new file mode 100644 index 0000000..185f274 --- /dev/null +++ b/04-algrithm/04-fibonacci/index.js @@ -0,0 +1,27 @@ +exports.fi = function fi(n) { + if (n === 0) return 1 + if (n === 1) return 1 + if (n === 2) return 2 + return fi(n - 1) + fi(n - 2) +} + +exports.fi2 = function fi2(n) { + if (n === 0) return 1 + if (n === 1) return 1 + if (n === 2) return 2 + var a = 1 + var b = 1 + var c = 2 + for (var i = 3; i <= n; i++) { + a = b + b = c + c = a + b + } + return c +} + + +// for (var i = 0; i < 10; i++) { +// console.log(fi2(i)) +// } + diff --git a/04-algrithm/05-coin/__tests__/index.spec.js b/04-algrithm/05-coin/__tests__/index.spec.js new file mode 100644 index 0000000..be1f6da --- /dev/null +++ b/04-algrithm/05-coin/__tests__/index.spec.js @@ -0,0 +1,12 @@ +const { check } = require('../index') +describe('杨辉三角', () => { + it('第1层 需要的2个硬币数', () => { + expect(check(1)).toBe(2) + }) + it('第2层 需要的8个硬币数', () => { + expect(check(3)).toBe(8) + }) + it('第45层 需要的3518437540个硬币数', () => { + expect(check(45)).toBe(3518437540) + }) +}) \ No newline at end of file diff --git a/04-algrithm/05-coin/index.js b/04-algrithm/05-coin/index.js new file mode 100644 index 0000000..f72a600 --- /dev/null +++ b/04-algrithm/05-coin/index.js @@ -0,0 +1,27 @@ +exports.check = function (n) { + function getCount(n) { + var coin = [10000, 5000, 2000, 1000, 500, 100, 50, 10, 5, 1] + result = 0 + for (var i = 0; i < coin.length; i++) { + var cnt = Math.floor(n / coin[i]) + n = n % coin[i] + result += cnt + } + return result + } + row = new Array(n + 1) + row[0] = 1 + for (var i = 1; i < n + 1; i++) { + row[i] = 0 + } + for (var i = 0; i < n; i++) { + for (var j = i + 1; j > 0; j--) { + row[j] += row[j - 1] + } + } + var total = 0 + for (var i = 0; i < n + 1; i++) { + total += getCount(row[i]) + } + return total +} \ No newline at end of file