Skip to content

Instantly share code, notes, and snippets.

@fireyy
Forked from anonymous/gist:792813
Created January 25, 2011 02:06
Show Gist options
  • Save fireyy/794394 to your computer and use it in GitHub Desktop.
Save fireyy/794394 to your computer and use it in GitHub Desktop.
<div class="box">
<div style="min-height: 73px;" class="cell">
<h1>
24点在线解答
</h1>
<div class="sep20">
</div>
<table height="600" cellspacing="0" cellpadding="0" border="0" align="center"
width="600" class="mbody">
<tbody>
<tr>
<td>
</td>
<td width="579" valign="top" rowspan="2">
<form onsubmit="return valid(this)" id="breast">
请输入四个整数:
<br>
<input class="sls" size="5" name="a1" maxlength="6">
<input class="sls" size="5" name="a2" maxlength="6">
<input class="sls" size="5" name="a4" maxlength="6">
<input class="sls" size="5" name="a8" maxlength="6">
<select style="display:none" name="count1">
<option value="1">
只找一个答案
</option>
<option value="5">
最多找5个
</option>
<option value="10">
最多找10个
</option>
<option value="0" selected="">
找全部答案
</option>
</select>
<div class="sep10"></div>
<input class="super normal button" type="submit" value="开始计算24点" name="submit">
<br>
<br>
&nbsp; 计算结果:&nbsp;&nbsp;&nbsp;
<br>
<textarea class="mll tall" rows="15" name="text" cols="30">
</textarea>
</form>
<div class="sep10"></div>
<form>
<input class="super normal button" type="submit" value="清除输入" oonclick="clean()"
name="提交">
</form>
</tbody>
</table>
<script language=javascript>
function tdisoper(f0, f1, f2, f3) {
this[0] = f0;
this[1] = f1;
this[2] = f2;
this[3] = f3;
}
disoper = new tdisoper("-", "+", "/", "*");
function oper(f, m, n) {
if (f == 3) return (m * n);
if (f == 2) return (m / n);
if (f == 1) return (parseFloat(m) + parseFloat(n));
if (f == 0) return (m - n);
}
function tb(i1, i2, i4, i8) {
this[1] = i1;
this[2] = i2;
this[4] = i4;
this[8] = i8;
}
function valid(F) {
n = F.count1.options[F.count1.selectedIndex].value;
if (isNaN(F.a1.value)) {
alert("必须输入整数!");
F.a1.focus();
return (false);
}
if (isNaN(F.a2.value)) {
alert("必须输入整数!");
F.a2.focus();
return (false);
}
if (isNaN(F.a4.value)) {
alert("必须输入整数!");
F.a4.focus();
return (false);
}
if (isNaN(F.a8.value)) {
alert("必须输入整数!");
F.a8.focus();
return (false);
}
b = new tb(F.a1.value, F.a2.value, F.a4.value, F.a8.value);
k = 0;
F.text.value = "";
for (i1 = 1; i1 <= 8; i1 *= 2) for (i2 = 1; i2 <= 8; i2 *= 2) for (i3 = 1; i3 <= 8; i3 *= 2) for (i4 = 1; i4 <= 8; i4 *= 2) {
if ((i1 | i2 | i3 | i4) != 0xf) continue;
for (f1 = 0; f1 <= 3; f1++) for (f2 = 0; f2 <= 3; f2++) for (f3 = 0; f3 <= 3; f3++) {
m = oper(f3, oper(f2, oper(f1, b[i1], b[i2]), b[i3]), b[i4]);
if (Math.abs(m - 24) < 1e-5) {
F.text.value = F.text.value + "((" + b[i1] + disoper[f1] + b[i2] + ")" + disoper[f2] + b[i3] + ")" + disoper[f3] + b[i4] + "\n";
if ((n != 0) && (++k >= n)) return (false);
}
m = oper(f1, b[i1], oper(f3, oper(f2, b[i2], b[i3]), b[i4]));
if (Math.abs(m - 24) < 1e-5) {
F.text.value = F.text.value + b[i1] + disoper[f1] + "((" + b[i2] + disoper[f2] + b[i3] + ")" + disoper[f3] + b[i4] + ")\n";
if ((n != 0) && (++k >= n)) return (false);
}
m = oper(f3, oper(f1, b[i1], oper(f2, b[i2], b[i3])), b[i4]);
if (Math.abs(m - 24) < 1e-5) {
F.text.value = F.text.value + "(" + b[i1] + disoper[f1] + "(" + b[i2] + disoper[f2] + b[i3] + "))" + disoper[f3] + b[i4] + "\n";
if ((n != 0) && (++k >= n)) return (false);
}
m = oper(f1, b[i1], oper(f2, b[i2], oper(f3, b[i3], b[i4])));
if (Math.abs(m - 24) < 1e-5) {
F.text.value = F.text.value + b[i1] + disoper[f1] + "(" + b[i2] + disoper[f2] + "(" + b[i3] + disoper[f3] + b[i4] + "))\n";
if ((n != 0) && (++k >= n)) return (false);
}
m = oper(f2, oper(f1, b[i1], b[i2]), oper(f3, b[i3], b[i4]));
if (Math.abs(m - 24) < 1e-5) {
F.text.value = F.text.value + "(" + b[i1] + disoper[f1] + b[i2] + ")" + disoper[f2] + "(" + b[i3] + disoper[f3] + b[i4] + ")\n";
if ((n != 0) && (++k >= n)) return (false);
}
}
}
F.text.value = F.text.value + "----END----\n"
return (false);
}
function newin(s, target, ww, hh) {
window.open(s, target, "menubar=0,resizable=1,scrollbars=1,width=" + ww + ",height=" + hh)
}
</script>
</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment