问题描述
我有一个整数数组
我想从该数组中检索最大数和最大数的索引,而无需使用Math.Max()函数..
最简单的方法是什么...
欢呼声
Santhosh; P; P
Hi,
I have an array of integers
I like to retrieve the max number and index of max number from that array without using Math.Max() function..
what is the easiest way...
Cheers
Santhosh ;P ;P
function insRows()
{
var x = document.getElementById('finalMarks').insertRow();
var cell = x.insertCell(0)
var a = x.insertCell(1);
var b = x.insertCell(2);
var c = x.insertCell(3);
var d = x.insertCell(4);
var element1 = document.createElement("input");
element1.type = "checkbox";
cell.appendChild(element1);
a.innerHTML = document.getElementById('txtqualify').options[document.getElementById('txtqualify').selectedIndex].text
b.innerHTML = document.getElementById('txtsem').value
c.innerHTML = document.getElementById('txtmaxmrk').value
d.innerHTML = document.getElementById('txtobtmrk').value
}
function function1()
{
var myRow = document.all.finalMarks.rows.length;
var ed='';
var mm=0,om=0, x,z;
for(i=0;i<myRow;i++)
{
r = document.all.finalMarks.rows[i];
pr = r.cells[1].innerHTML
mm+= parseInt(r.cells[3].innerHTML)
om+= parseInt(r.cells[4].innerHTML)
if (pr!=ed && ed!='')//(i==myRow-1)
{
prevr = document.all.finalMarks.rows[i-1];
mycurrent_cell = document.createElement("td");
mm-= parseInt(r.cells[3].innerHTML)
om-= parseInt(r.cells[4].innerHTML)
z = (om/mm)*100
mycurrent_cell.innerHTML = z
mm= parseInt(r.cells[3].innerHTML)
om= parseInt(r.cells[4].innerHTML)
alert(mycurrent_cell.innerHTML + '[' + pr + ']' + ed)
//alert(!isNaN(mycurrent_cell.innerHTML))
if(!isNaN(mycurrent_cell.innerHTML))
{
qualary[qualary.length] = prevr.cells[1].innerHTML
pctary[pctary.length] = mycurrent_cell.innerHTML
prevr.appendChild(mycurrent_cell);
mycurrent_cell.innerHTML = z;
document.getElementById('txtpert').value = z
}
}
ed = r.cells[1].innerHTML
alert(ed)
if (i==myRow-1)
{
prevr = document.all.finalMarks.rows[i];
//mycurrent_cell = document.createElement("td");
x = (om/mm)*100
mycurrent_cell.innerHTML = x
qualary[qualary.length] = prevr.cells[1].innerHTML
pctary[pctary.length] = mycurrent_cell.innerHTML
prevr.appendChild(mycurrent_cell);
if(!isNaN(x))
document.getElementById('txtperct').value = x
}
}
//document.frmMarks.txtper.value = Math.max( x, z)
var str = sortAndReturn(pctary,qualary)
//alert(pctary + '$' + qualary) //alert(sortAndReturn(pctary,qualary))
//alert(str) //alert(str[0]+','+str[1])
document.getElementById('degr').value = str.split('$')[0]
document.getElementById('txtmarks').value = str.split('$')[1]
}
function sortAndReturn(pctary,qualary)
{
var myArray3 = new Array();
for(i=0;i<pctary.length;i++)
myArray3[i]=pctary[i];
sort(pctary)
//alert(sort(pctary)) //alert(myArray3)
return qualary[indexSearch(myArray3,pctary[0])] + '$' + pctary[0]
}
function sort(a)
{
var max;
for(i=0;i<a.length;i++)
{
max = i;
for(j=i+1;j<a.length;j++)
{
if (a[j]>a[max])
{
max=j;
}
}
//alert(a[max])
if (i != max)
{
var swap = a[i];
a[i] = a[max];
a[max] = swap;
}
}
}
function indexSearch(a,b)
{
for(i=0;i<a.length;i++)
if(a[i]==b)
{return i ;}
}
HTML代码:
HTML CODE:
<table BORDER ="6" width="85%"align="center" bgcolor="#00FFFF" style="font-family: Verdana;color: black;font-size: 12px">
<tr>
<td align="center" width="25%" height="40"> Qualification:</td>
<td align="left" style="WIDTH:25%" >
<select id ="txtqualify" style ="width:150px;height:25px" name ="txtqualify" > </select> </td>
<td align="center" width="25%" height="40">Semester:</td>
<td> <input type = "text" SIZE="8" maxlength = "3" id = "txtsem" name = "txtsem"> </td>
</tr>
<tr>
<td align= "center" width ="25%" height="40">Max.Marks:</td>
<td> <input type = "text" SIZE="8" maxlength = "3" id = "txtmaxmrk" name = "txtmaxmrk"> </td>
<td align= "center" width ="25%" height="40">Obtained.Marks:</td>
<td> <input type = "text" SIZE="8" maxlength = "3" id = "txtobtmrk" name = "txtobtmrk"> </td>
</tr>
<tr>
<td align= "center" width ="25%" height="40">Percentage:</td>
<td> <input type = "text" SIZE="8" maxlength = "5" id = "txtper" readonly name = "txtper">
<td align="center" colspan="4" valign="middle">
<input type="RESET" align="middle" style="width:20%" value="ADD" onclick="insRows()" >
<input type="button" align="middle" style="width:20%" value="REMOVE" onclick="delRow()" >
<input type="button" align="middle" style="width:20%" value="CLOSE" onclick="" >
<input type="button" align="middle" style="width:20%" value="SUM" onclick="function1()" >
</td>
</tr>
<tr>
<td align="center" height="40"> <input type = "text" SIZE="8" maxlength = "5" id = "txtpert" name = "txtpert"> </td>
<td align="center"> <input type = "text" SIZE="8" maxlength = "5" id = "txtperct" name = "txtperct"> </td>
<td align="center"> <input type = "text" SIZE="10" maxlength = "5" readonly id = "degr" name = "degr"> </td>
</tr>
</table>
<table id ="finalMarks" BORDER ="6" width="85%"align="center" bgcolor="#00FFFF" style="font-family: Verdana;color: black;font-size: 12px">
<thead>
<th height="40">Remove</th>
<th>Qualification</th>
<th>Sem/Year</th>
<th>Max.Marks</th>
<th>Obt.Marks</th>
<th>Percentage of Marks:</th>
</thead>
</table>
==============
来自JSOP:这看起来像是作业.我怎么知道?因为您的要求是荒谬的.除非在使用该语言/框架的功能方面存在广为人知且未解决的问题,否则任何有主意的人都不会故意避免使用所选语言/框架的功能.由于内置的Math.Max方法没有任何问题,因此这是功课.
接下来,您无需发布HTML代码(可能是您发布的C#代码的90%)即可提出此问题.标题为"Quick Answers"是有原因的.
最后,思考问题,并尝试提出尽可能多的方法来解决问题,然后选择您认为是最好的方法.您的老师很乐意嘲笑您的选择.
===============
From JSOP: This looks like homework. How can I tell? Because you''re requirements are absurd. Nobody in their right mind would purposely avoid using features of the chosen language/framework unless there was a widely known and un-patched problem with said feature. Since there''s nothing wrong with the built in Math.Max method, this is homework.
Next, you don''t need to post the HTML code (and probably 90% of the C# code you posted) to ask this question. It''s title "Quick Answers" for a reason.
Lastly, THINK about the problem, and try to come up with as many ways to address the issue as you can, and then pick the one YOU think is the best. Your instructor will be happy to ridicule your choice.
推荐答案
这篇关于如何找到最大值和最大值索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!