杨氏矩阵查找的JS代码
时间:2021-07-01 10:21:17
帮助过:2人阅读
代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>杨氏矩阵查找</title>
</head>
<body>
<script language="javascript">
function xunhuan()
{
var m = parseInt(document.getElementsByTagName("input")[0].value)||document.getElementsByTagName("input")[0].getAttribute("value");
var n = parseInt(document.getElementsByTagName("input")[1].value)||document.getElementsByTagName("input")[1].getAttribute("value");
var t = parseInt(document.getElementsByTagName("input")[2].value)||document.getElementsByTagName("input")[2].getAttribute("value");
if(m >= 1&&n <= 1000&&t >= 1&&t <= 1000000)
{
var num = 2;
for(var x = 0;x < m;x++)
{
for(var y = 0;y < n;y++)
{
document.write((num++)+" ");
}
document.write("<br/>");
}
var a=new Array();
for(var e = m*n+1;e > 1; e--)
{
a.splice(0,0,e)
}
chazhao(a,t);
}
else
{
alert("不符合要求,请重新输入");
}
}
function chazhao(a,number)
{
document.write(isCon(a, number));
function isCon(a, number)
{
for(var i=0; i<a.length; i++)
{
if(a[i] === number)
{
return String(true);
}
}
return String(false);
}
}
</script>
<form>
<p>矩阵行数:<input type="text" name="矩阵行数" value=3 /> 矩阵列数:<input type="text" name="矩阵列数" value=3 /></p>
<p>查询数字:<input type="text" name="查询数字" value=1 /></p>
<input type="button" onclick="xunhuan()" value="数组" />
</form>
</body>
</html>