Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * 把N个乒乓球放入一个箱子,每次只能取1个或2个,有多少种取法?
- */
- var pingpong = function (n) {
- if (n <= 0) return 0;
- if (n === 1) {
- return 1;
- } else {
- var sum = 0, tail;
- // handler 1
- sum = sum + pingpong(n - 1);
- // handler 2
- tail = n - 2;
- if (tail > 0) {
- sum = sum + pingpong(tail);
- } else {
- sum = sum + 1;
- }
- return sum;
- }
- }
- module.exports = pingpong;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement