快算24在這裡驗算

遊戲音樂影視動漫
Post Reply
ejsoon
Site Admin
Posts: 3808
Joined: 2016 Jan 10, 22:15

快算24在這裡驗算

Post by ejsoon » 2018 Aug 29, 10:14

本論壇把代碼拿來做了個網頁


https://www.thinkcalculator.com/numbers/24game.php

Code: Select all

<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8">
		<title>24 game Calculator</title>
	</head>
	<body>
		<div class="left_oth">
			<div id="calculator">
				<h2> 24 game Calculator </h2>
				<SCRIPT language=javaScript>
function clean() {
	breast.reset();

}
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("You must enter an integer!");    F.a1.focus();    return(false);}
	if (isNaN(F.a2.value)){alert("You must enter an integer!");    F.a2.focus();    return(false);}
	if (isNaN(F.a4.value)){alert("You must enter an integer!");    F.a4.focus();    return(false);}
	if (isNaN(F.a8.value)){alert("You must enter an integer!");    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+"----There is no more!----\n"
	return(false);
}
function newin(s,target,ww,hh) 
{ window.open(s,target,"menubar=0,resizable=1,scrollbars=1,width="+ww+",height="+hh)}
				</SCRIPT>
				<center><form id="breast" onsubmit="return valid(this)">
						<b>Please input four Integers</b>:<br>
						<input type="text" maxlength="6" name="a1" size="5">
						<input type="text" maxlength="6" name="a2" size="5">
						<input type="text" maxlength="6" name="a4" size="5">
						<input type="text" maxlength="6" name="a8" size="5">
						<select name="count1">
							<option value="1">Find one answer!</option>
							<option selected="" value="5">Find five answers!</option>
							<option value="10">Find ten answers!</option>
							<option value="0">Find all answers!</option>
						</select> <br>
						<input name="submit" type="submit" value="Calculate" class="calc">&nbsp;&nbsp;&nbsp;&nbsp;<input name="提交" type="reset" onclick="clean();" value="Reset">
						<table><tbody><tr><td><b>Result</b>:<br><textarea name="text" rows=8 cols=50></textarea></td><td>
									</td></tr></tbody></table>
					</form></center>
			</div>
		</div>
	</body>
</html>

ejsoon
Site Admin
Posts: 3808
Joined: 2016 Jan 10, 22:15

快算24經典題

Post by ejsoon » 2018 Sep 14, 15:02

運用分數:
5,5,5,1
4,4,7,7(3,3,7,7)
1,4,5,6(實在巧妙!)
2,3,5,12(刷新難度)
5,7,7,11

先加減後乘除最後加減:
2,3,7,9
5,5,5,12

乘成大數再減:
5,8,9,13
3,7,9,13
5,6,6,9

先乘除後加減最後乘除:
2,2,10,11
4,4,10,10(大數)

先加再除最後乘:
6,7,8,12

先除而後加:
4,10,11,12

ejsoon
Site Admin
Posts: 3808
Joined: 2016 Jan 10, 22:15

Re: 快算24在這裡驗算

Post by ejsoon » 2018 Sep 19, 12:27

自製24點和猜數字

難點:JS數組亂序

Code: Select all

function sortNumber(a,b) {
 return Math.random() - 0.5; 
} 
var arr = arr=[9,3,1,2,5,8,4,7,6,0]; 
arr.sort(sortNumber);

Code: Select all

function shuffle(arr) {
  var i = arr.length, t, j;
  while (i) {
    j = Math.floor(Math.random() * i--);
    t = arr[i];
    arr[i] = arr[j];
    arr[j] = t;
  }
}
國外https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array

還要做:
24點:給出解答或證明無解(這個可以不做,因爲上面的網站已經做好了)
猜數字:評分

Post Reply