<html>
<head>
<meta http-equiv="Content-Type" content="text/htmlcharset=gb2312">
<title>秒表 Stopwatch for Rubik's Cube China</title>
</head>
<style>
td,input,div,th{font:9pt verdana}
p {line-height:60%}
</style>
<body onkeyup="begin(event)" onkeydown="stop(event)" onload=rndCube()>
<script type=text/javascript>
var Max=25
var bestAve
var then
var nowDec
var nowBest
var nowWorst
var runing=0
var timeint
var i=0
var totalsec
var sec
var min
var subsec
var secdis
var mindis
var subsecdis
var datas=new Array()
var nowSecondes
function getAv(st,ed)
{
for(i=sti<=edi++)
{
datas.join(" ")
}
}
function stop(evt)
{
if (runing==1 &&evt.keyCode==32)
clearTimeout(timeint)
}
function toSeconds(dec)
{ var Ms,sec
var tmpDec=dec.split(":")
if(tmpDec[0]*10/10>0)
{
Ms=tmpDec[0]*60
return(Ms+tmpDec[1]*10/10)
}else{
return(tmpDec[1]*10/10)
}
}
function begin(evt)
{
if (evt.keyCode==32)
{
if (runing==0){
then=new Date()
runing=1
show()
}
else if (runing==1)
{
runing=2
}
else
{
ShowDatas(toSeconds(nowSeconds))
document.getElementById("watch").innerHTML="00:00.00"
runing=0
}
}
}
var a=0
function ShowDatas(dec)
{
if(dec==0){return false}
var newDec=get2(dec)
datas.push(newDec) //添加记录
SortDatas() //排序得到最大最小值
ShowDetails()
spanBest.innerHTML="<b style=color:red>"+sec2minsec(nowBest)+"</b>"
spanWorst.innerHTML="<b>"+sec2minsec(nowWorst)+"</b>"
var tmp1=0
for(i=0i<datas.lengthi++)
{
tmp1+=datas[i]
}
spanAv.innerHTML="<b>"+sec2minsec(get2(tmp1/datas.length))+"</b>"
}
var bestAve=new Array()
function sec2minsec(n)
{ if (n>60)
{
var tmpmins=Math.floor(n/60)
var tmpseconds=get2(n-tmpmins*60)
var tmpstring
if (tmpseconds<10)
{ tmpstring=tmpmins+":0"+tmpseconds
}
else tmpstring=tmpmins+":"+tmpseconds
return tmpstring
}
else return n
}
function ShowDetails()
{
if(datas.length>12){a=datas.length -12}
var tmpTd="<table cellspacing=1 bgcolor=olive><tr align=center bgcolor=EEEEEE><th width=50>1<th width=50>2<th width=50>3<th width=50>4<th width=50>"
tmpTd+="5<td width=50>6<th width=50>7<th width=50>8<th width=50>9<th width=50>10<th width=50>11<th width=50>12</tr><tr bgcolor=white>"
for(i=ai<(12+a)i++)
{
bestAve[i-a]=datas[i]//将当前12局成绩存入数组
tmpTd+="<td align=center>"
if(datas[i]==undefined){tmpTd+="-"}
else
{
if(datas[i]==nowBest || datas[i]==nowWorst)
{
tmpTd+=sec2minsec(datas[i])+"*"
}else
{
tmpTd+=sec2minsec(datas[i])
}
}
tmpTd+="</td>"
}
tmpTd+="</tr></table>"
//alert(datas.join(","))
div1.innerHTML=""
div1.innerHTML=tmpTd
rndCube()
document.focus()
}
function show()
{ var now=new Date()
diff=now.getTime()-then.getTime()
totalsec=Math.floor(diff/1000)
sec=totalsec%60
if(sec<10)
{
secdis="0"+sec
}
else
{
secdis=sec
}
min=(totalsec-sec)/60
if (min<10)
{
mindis="0"+min
}
else
{
mindis=min
}
subsec=Math.floor((diff%1000)/10)
if (subsec<10)
{
subsecdis="0"+subsec
}
else
{
subsecdis=subsec
}
nowSeconds=mindis+":"+secdis+"."+subsecdis
document.getElementById("watch").innerHTML=nowSeconds
timeint=setTimeout("show()",50)
}
function get2(dec) //取两位小数点
{
return Math.round(dec*100)/100
}
function SortDatas()//最大最小值
{
var tmpArr=new Array()
for(i=0i<datas.lengthi++)
{
tmpArr[i]=datas[i]
}
tmpArr.sort(function(a,b){ return a-b})
nowBest=tmpArr[0]
nowWorst=tmpArr[tmpArr.length-1]
ShowBestAv()
}
function mycls()
{
clearTimeout(timeint)
nowSeconds="00:00:00"
runing=0
datas=new Array()
ShowDatas(0)
ShowDetails()
document.getElementById("watch").innerHTML="00:00.00"
btn1.focus()
}
function ShowBestAv()
{
var av=0
bestAve.sort()
if(bestAve[11]==undefined){return false}
for(i=1i<bestAve.length-1i++)
{
av+=bestAve[i]
}
spanBestAv.innerHTML=get2(av/10)
//alert(bestAve)
}
</script>
<script>
function cancel()
{ clearTimeout(timeint)
nowSeconds="00:00:00"
runing=0
btn1.focus()
rndCube()
document.getElementById("watch").innerHTML="00:00.00"
}
function rndCube()
{
var move=""
var rndMove=new Array("R","L","F","B","U","D")
var add=0
var tmpRnd
var arr=new Array()
while(true)
{
if(add>=Max){break}
//tmpRnd=Math.round(Math.random()*5)
if(tmpRnd==arr[arr.length -1])
{ tmpRnd=Math.floor(Math.random()*6)
}
else
{ arr.push(tmpRnd)
add++
}
}
for(i=0i<arr.lengthi++)
{
var tmp=Math.floor(Math.random()*5)
if(tmp==4)
{ move+=rndMove[arr[i]]+"2"}
else if(tmp==2 || tmp==3)
{ move+=rndMove[arr[i]]+"'"}
else
{ move+=rndMove[arr[i]]}
move+=" "
}
rndDiv.innerHTML=move
}
</script>
<center>
<div id="watch" style="filter:shadraw(x=1,y=1,color=black)position:relativewidth: 359pxheight: 80px font-size:48ptfont-family:Arialfont-weight:boldcolor:navytext-align:center" >00:00.00</div>
<p>
<font size="2">用<font color="#000080"><b>空格</b></font>键<b>开始</b>(松开时触发)、<b>停止</b>(按下时触发)、<b>复位</b>(同时记录成绩),enjoy:)</font></p>
<p><b><font size="2" color="#FF0000">注意:</font></b><font size="2">请先最大化窗口或拉伸窗口使右边的滚动条不出现,否则空格键会让窗口滚动。</font></p>
<p><font size="2">下面的记录系统是魔方吧的MAN大侠编写。</font>
</p>
<div id=rndDiv style="font:12pt Arialfont-weight:bold"></div>
<input type=button value="清除所有记录" onclick=mycls()><input type=button value="不记录此次成绩" type=button onclick=cancel()div1.focus()>
<input type=button value="" style="width:0pxheight:0px" onclick="document.focus" id=btn1>
<div id=div1>
<table cellspacing=1 bgcolor=olive>
<tr bgcolor=#EEEEEE align=center>
<td width=50>1
<td width=50>2
<td width=50>3
<td width=50>4
<td width=50>5
<td width=50>6
<td width=50>7
<td width=50>8
<td width=50>9
<td width=50>10
<td width=50>11
<td width=50>12
</tr>
<tr bgcolor=white align=center>
<td width=50>-
<td width=50>-
<td width=50>-
<td width=50>-
<td width=50>-
<td width=50>-
<td width=50>-
<td width=50>-
<td width=50>-
<td width=50>-
<td width=50>-
<td width=50>-
</tr>
</table>
</div>
</center>
<table align=center>
<tr><td>最快:</td><td><span id=spanBest></span></td></tr>
<tr><td>最慢:</td><td><span id=spanWorst></span></td></tr>
<tr><td>平均:</td><td><span id=spanAv></span></td></tr>
<tr><td>最好平均:</td><td><span id=spanBestAv></span></td></tr>
</table>
</body>
</html>
算法一:function getPrimeNumbers(limit){
var result = [2]
var is
if(limit <2)
return []
for(var i = 3, si <= limiti += 2){
is = true
s = Math.sqrt(i)
for(var j = 0, r, l = result.lengthj <= lj ++){
r = result[j]
if(r >s)
break
if(i % r)
continue
is = false
break
}
is &&result.push(i)
}
return result
}然后直接调用即可
算法二:
程序代码
var stopwatch = new Date() // 计时器, 初始化.
var MaxNum = 100000 // 查找 2到MaxNum 这范围内的素数 ( MaxNum 要>= 2 ).
var i, j // 计数器.
var count = 1// 初始化素数的个数, 因为我们从2开始计, 所以初始化为 1.
var PrimeTemp = [] // 在这个数组做记号, 做了记号的, 全不是素数.
var PrimeArys = [2] // 贮存素数的 数组. 因为 MaxNum >= 2, 所以第一个数组元素的值为 2 .
var oNum = Math.ceil( Math.sqrt( MaxNum ) ) // 为什么用 开方? 看到下面2个 for 没.
// 把不是素数的做 "记号".
for(i=3i<oNumi+=2)// +=2 ??? 我们整个程序都不用双数, 全用单数, 这样就快2倍了.
if( PrimeTemp[i]==null ) // 初始化 PrimeTemp 的数组, 数组里面当然什么都没有.
for(j=ii*j<=MaxNumj+=2) // i 的 j 倍一定不是素数, 但我们要 i*j 来看看是否超过了 MaxNum
PrimeTemp[ i*j ] = 0// 初始化 PrimeTemp 里的元素, 现在来帮它们做一个 "记号". 因为这个元素"不是"素数.
// 输出素数了.
for(i=3i<=MaxNumi+=2) // +=2 ,不要忘记, 我们不用双数的.
if( PrimeTemp[i]==null ) // 如果是 true , 这就表明 这个没有被做 "记号" , 所以它是 素数.
PrimeArys[ count++ ] = i // 是 素数 的话, 就存入 PrimeArys 数组.
document.write( PrimeArys.join(" ") , "<br><br>从2到"+MaxNum+"共有素数 "+count+" 个。") // 用 join()提高输出效率
var t=new Date()-stopwatch
alert("本次运行了 "+t+" 毫秒。")//看看 程序运行了多久.
<html><head><script type="text/javascript">function startTime(){var today=new Date()var h=today.getHours()var m=today.getMinutes()var s=today.getSeconds()// add a zero in front of numbers<10m=checkTime(m)s=checkTime(s)document.getElementById('txt').innerHTML=h+":"+m+":"+st=setTimeout('startTime()',500)} function checkTime(i){if (i<10) {i="0" + i} return i}</script></head><body onload="startTime()"><div id="txt"></div></body></html>