本文发表在 rolia.net 枫下论坛<HTML>
<head>
<script lang="javascript">
var strIndex = "";
var strInput;
var targetNumber;
var strTemp = "";
var nInd = 0;
function getNextNumber(N, a, b, m){
for(var i=m; i<=N-(b-a+1); i++){
strIndex += i;
if(a==b){
//Got the indexes
document.getElementById("output").innerHTML += (strIndex + ": ");
//insert '+' based on the indexes
strTemp = strInput;
for(var j=strIndex.length-1; j>=0; j--){
nInd = parseInt(strIndex.charAt(j));
strTemp = strTemp.substr(0, nInd) + '+' + strTemp.substring(nInd, strTemp.length);
}
document.getElementById("output").innerHTML += (strTemp + "<BR>");
//
if (eval(strTemp) == targetNumber) alert("Got it! " + strTemp + "=" + targetNumber + "\nResult: " + b);
strIndex = strIndex.substr(0, strIndex.length-1);
}else if(a<b){
getNextNumber(N, a+1, b, i+1);
}
}
strIndex = strIndex.substr(0, a-2);
}
function test(){
document.getElementById("output").innerHTML ="";
strInput = document.getElementById('txtString').value;
targetNumber = parseInt(document.getElementById('txtNumber').value);
for (var k=1; k<strInput.length; k++){
getNextNumber(strInput.length, 1, k, 1);
}
}
</script>
</head>
<body>
<form>
<table>
<tr>
<td>Digit String:<input type=text name=txtString id=txtString size=20></td>
<td>Target Number:<input type=text name=txtNumber id=txtNumber size=20></td>
</tr>
<tr>
<td> </td>
<td><input type=button name=btnTest value=" Do It " onclick="test();"></td>
</tr>
<tr>
<td colspan=10><div id=output style="width:200px; height:200px; overflow:auto"></div></td>
</tr>
</table>
</form>
</body>
</HTML>更多精彩文章及讨论,请光临枫下论坛 rolia.net
<head>
<script lang="javascript">
var strIndex = "";
var strInput;
var targetNumber;
var strTemp = "";
var nInd = 0;
function getNextNumber(N, a, b, m){
for(var i=m; i<=N-(b-a+1); i++){
strIndex += i;
if(a==b){
//Got the indexes
document.getElementById("output").innerHTML += (strIndex + ": ");
//insert '+' based on the indexes
strTemp = strInput;
for(var j=strIndex.length-1; j>=0; j--){
nInd = parseInt(strIndex.charAt(j));
strTemp = strTemp.substr(0, nInd) + '+' + strTemp.substring(nInd, strTemp.length);
}
document.getElementById("output").innerHTML += (strTemp + "<BR>");
//
if (eval(strTemp) == targetNumber) alert("Got it! " + strTemp + "=" + targetNumber + "\nResult: " + b);
strIndex = strIndex.substr(0, strIndex.length-1);
}else if(a<b){
getNextNumber(N, a+1, b, i+1);
}
}
strIndex = strIndex.substr(0, a-2);
}
function test(){
document.getElementById("output").innerHTML ="";
strInput = document.getElementById('txtString').value;
targetNumber = parseInt(document.getElementById('txtNumber').value);
for (var k=1; k<strInput.length; k++){
getNextNumber(strInput.length, 1, k, 1);
}
}
</script>
</head>
<body>
<form>
<table>
<tr>
<td>Digit String:<input type=text name=txtString id=txtString size=20></td>
<td>Target Number:<input type=text name=txtNumber id=txtNumber size=20></td>
</tr>
<tr>
<td> </td>
<td><input type=button name=btnTest value=" Do It " onclick="test();"></td>
</tr>
<tr>
<td colspan=10><div id=output style="width:200px; height:200px; overflow:auto"></div></td>
</tr>
</table>
</form>
</body>
</HTML>更多精彩文章及讨论,请光临枫下论坛 rolia.net