1 // 小熊分苹果问题 2 function apple(n) { 3 var i = 1, final = 1; 4 var temp = n; 5 var sum; 6 7 while (temp > 1) { 8 var tmp1 = ((i * n) + 1), tmp2 = (n - 1); 9 var num = tmp1 / tmp2; 10 11 if (tmp1 % tmp2 != 0) { 12 ++final; 13 i = final; 14 temp = n; 15 continue; 16 } 17 18 i = num, temp--; 19 } 20 21 return i * n + 1; 22 } 23 console.log(apple(5)); 24 25 // 年终奖问题 26 27 function findMax(x, y, sum) { 28 sum += arr[x][y]; 29 if ((x == 5) && (y == 5) && (sum > max)) { 30 max = sum; 31 } 32 if (x < 5) { 33 findMax(x + 1, y, sum); 34 } 35 if (y < 5) { 36 findMax(x, y + 1, sum); 37 } 38 } 39 40 var arr = [ 41 [200, 120, 400, 150, 180, 300], 42 [150, 250, 360, 120, 200, 130], 43 [350, 300, 250, 100, 500, 260], 44 [100, 150, 260, 320, 100, 150], 45 [500, 130, 260, 100, 200, 170], 46 [160, 100, 250, 200, 600, 200] 47 ]; 48 var max = 0; 49 findMax(0, 0, 0); 50 console.log(max);