看到一个小学四年级的数学题,说是一共4个人过桥,分别单独过桥用时3,4,6,9分钟,该桥每次只能过两个人,且每次过去之后必须要一个人回去接人,问四个人全部过去的最短时间。
真实题目确实很长,我不知道小学生遇到这个问题能否耐心读完读懂该问题。
所以我觉得题目确实蛮有意思的,有点脑筋急转弯的感觉。
如果静下心来解决,用排除法也能很快得到答案。
另外用Javascript穷尽法也能得到答案,网上还有人问js怎么写。
能力有限,用的比较死板的解决办法,应该有大神有更好的解决办法。
/// <reference path="jquery-1.10.2.js" /> $(document).ready(function () { $('#CrossBridge').html(); HowToCrossBridge(3, 4, 6, 9); }); /** //console.log("第一次过桥由" + firstGo1 + "和" + firstGo2 + "过,共计" + firstGo2 + "分钟"); //document.writeln("第一次过桥由" + firstGo1 + "和" + firstGo2 + "过,共计" + firstGo2 + "分钟"); 知识点: 1.数组的copy 注意区分深拷贝和浅拷贝 array.slice(0)以及array.concat() 2.数组的remove和push以及sort **/ function HowToCrossBridge(a, b, c, d) { var totalTime1 = 0; var breakFlag = true; var str1 = ""; var count = 0; var minTime = 0; var minStr = ""; //while (breakFlag) { //totalTime1 = 0; var waitingCross = [a, b, c, d]; var hadCrossed = new Array(); waitingCross = waitingCross.sort(function (a, b) { return a - b });//按顺序从小到大排序 //第一次过桥(过去2人,剩余2人) for (var i = 0; i < 4; i++) { for (var j = i + 1; j < 4; j++) { //count += 1; totalTime1 = 0; var hadCrossed1 = hadCrossed.concat(); var waitingCross1 = waitingCross.concat(); var firstGo1 = waitingCross1[i]; var firstGo2 = waitingCross1[j]; totalTime1 = totalTime1 + firstGo2; str1 = "<p>第一次过桥由" + firstGo1 + "和" + firstGo2 + "过,共计<fond style='color:red'>" + firstGo2 + "</fond>分钟,合计<fond style='color:red'>" + totalTime1 + "</fond>分钟</p>"; //移除等待过桥人员 waitingCross1.remove(firstGo1); waitingCross1.remove(firstGo2); waitingCross1.sort(function (a, b) { return a - b }); //添加已过桥人员 hadCrossed1 = [firstGo1, firstGo2]; hadCrossed1.sort(function (a, b) { return a - b }); str1 += "<p>第一次过桥后 hadCrossed=" + hadCrossed1.join() + " waitingCross=" + waitingCross1.join() + "</p>"; //第二次返回(过去1人,剩余3人) for (var k = 0; k < 2; k++) { //count += 1; var hadCrossed2 = hadCrossed1.concat(); var waitingCross2 = waitingCross1.concat(); var str2 = str1; var totalTime2 = totalTime1; var secondBack = hadCrossed2[k]; totalTime2 = totalTime2 + secondBack; str2 += "<p>第二次返回由" + secondBack + "过,共计<fond style='color:red'>" + secondBack + "</fond>分钟,合计<fond style='color:red'>" + totalTime2 + "</fond>分钟</p>"; //添加等待过桥人员 waitingCross2.push(secondBack); waitingCross2.sort(function (a, b) { return a - b }); //移除已过桥人员 hadCrossed2.remove(secondBack); hadCrossed2.sort(function (a, b) { return a - b }); str2 += "<p>第二次返回后 hadCrossed=" + hadCrossed2.join() + " waitingCross=" + waitingCross2.join() + "</p>"; //第三次过桥(过去3人,剩余1人) for (var m = 0; m < 3; m++) { for (var n = m + 1; n < 3; n++) { //count += 1; var str3 = str2; var hadCrossed3 = hadCrossed2.concat(); var waitingCross3 = waitingCross2.concat(); var totalTime3 = totalTime2; var thirdGo1 = waitingCross3[m]; var thirdGo2 = waitingCross3[n]; totalTime3 = totalTime3 + thirdGo2; str3 += "<p>第三次过桥由" + thirdGo1 + "和" + thirdGo2 + "过,共计<fond style='color:red'>" + thirdGo2 + "</fond>分钟,合计<fond style='color:red'>" + totalTime3 + "</fond>分钟</p>"; //移除 waitingCross3.remove(thirdGo1); waitingCross3.remove(thirdGo2); waitingCross3.sort(function (a, b) { return a - b }); hadCrossed3.push(thirdGo1); hadCrossed3.push(thirdGo2); hadCrossed3.sort(function (a, b) { return a - b }); str3 += "<p>第三次过桥后 hadCrossed=" + hadCrossed3.join() + " waitingCross=" + waitingCross3.join() + "</p>"; //第四次返回(过去2人,剩余2人) for (var l = 0; l < 2; l++) { count += 1; var hadCrossed4 = hadCrossed3.concat(); var waitingCross4 = waitingCross3.concat(); var str4 = str3; var totalTime4 = totalTime3; var fouthBack = hadCrossed4[l]; totalTime4 = totalTime4 + fouthBack; str4 += "<p>第四次返回由" + fouthBack + "过,共计<fond style='color:red'>" + fouthBack + "</fond>分钟,合计<fond style='color:red'>" + totalTime4 + "</fond>分钟</p>"; //添加 waitingCross4.push(fouthBack); waitingCross4.sort(function (a, b) { return a - b }); //移除 hadCrossed4.remove(fouthBack); hadCrossed4.sort(function (a, b) { return a - b }); str4 += "<p>第四次返回后 hadCrossed=" + hadCrossed4.join() + " waitingCross=" + waitingCross4.join() + "</p>"; //第五次过桥(全部通过) var hadCrossed5 = hadCrossed4.concat(); var waitingCross5 = waitingCross4.concat(); var totalTime5 = totalTime4; var fifthGo1 = waitingCross5[0]; var fifthGo2 = waitingCross5[1]; totalTime5 = totalTime5 + fifthGo2; str4 += "<p>第五次过桥由" + fifthGo1 + "和" + fifthGo2 + "过,共计<fond style='color:red'>" + fifthGo2 + "</fond>分钟,合计<fond style='color:red'>" + totalTime5 + "</fond>分钟</p>"; //添加 waitingCross5.remove(fifthGo1); waitingCross5.remove(fifthGo2); waitingCross5.sort(function (a, b) { return a - b }); //移除 hadCrossed5.push(fifthGo1); hadCrossed5.push(fifthGo2); hadCrossed5.sort(function (a, b) { return a - b }); str4 += "<p>第五次过桥后 hadCrossed=" + hadCrossed5.join() + " waitingCross=" + waitingCross5.join() + "</p>"; str4 += "<p><fond style='color:red'>本方案共计" + totalTime5 + "分钟</fond></p>"; var str5 = "方案" + count + str4; str5 += "<p>==================================================================</p>"; $('#CrossBridge').append(str5); if (minTime > 0) { if (totalTime5 < minTime) { minStr = str5; } } else { minTime = totalTime5; minStr = str5; } } } } } } } //} $('#CrossBridge').append("<p>/********************************************最短时间方案为**************************************************************/</p>"); $('#CrossBridge').append(minStr); } /* 数组中随机取几个元素 var items = ['1','2','4','5','6','7','8','9','10']; console.log( getRandomArrayElements(items, 4) ); 数组中随机获取一个元素 var item = items[Math.floor(Math.random()*items.length)]; */ function getRandomArrayElements(arr, count) { var shuffled = arr.slice(0), i = arr.length, min = i - count, temp, index; while (i-- > min) { index = Math.floor((i + 1) * Math.random()); temp = shuffled[index]; shuffled[index] = shuffled[i]; shuffled[i] = temp; } return shuffled.slice(min); } Array.prototype.indexOf = function (val) { for (var i = 0; i < this.length; i++) { if (this[i] == val) return i; } return -1; } Array.prototype.remove = function (val) { var index = this.indexOf(val); if (index > -1) { this.splice(index, 1); } }