Home  >  Article  >  Backend Development  >  How to use php to implement the judges' scorer, php to implement the judges' scoring_PHP tutorial

How to use php to implement the judges' scorer, php to implement the judges' scoring_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 09:45:211481browse

How to use php to implement the judge's scorer, php to implement the judge's scoring

Use the selection sort method in the first step to find the highest score and the lowest score from the input array, and then remove the highest score points and a minimum score to get the player's average score.

1. Implementation code

<&#63;php
function fairScore(&$arr)
{ //选择排序法的第一步,这里只需要找到这个数组中的最大值和最小值即可,没必要对整个数组排序
 $minVal = $arr[0];
 $minIndex = 0;
 $maxVal = $arr[0];
 $maxIndex = 0;
 $sum = 0;
 for ($i=1;$i<count($arr);$i++)
 {
  if ($arr[$i]<$minVal)
  {
   $minVal = $arr[$i];;
   $minIndex = $i;
  }
  if ($arr[$i]>$maxVal)
  {
   $maxVal = $arr[$i];
   $maxIndex = $i;
  }
 }
 echo "最高分是:".$maxVal." 最低分是:".$minVal."<br/>";
 for ($i=0;$i<count($arr);$i++)
 {
  $sum +=$arr[$i];
 }
 $sum -=($minVal+$maxVal);
 echo "一共有".count($arr)."个评委,去掉最高分和最低分后的平均分是".$sum/(count($arr)-2);
}
 
$score = @$_REQUEST['score'];//第一次打开页面时会因为没有输入数据,所以没法提交。这里加@是为了去除浏览器notice警告
$aScore = explode(" ",$score);
 
&#63;>
 
<html>
<head>
<title>去掉最高分和最低分后的平均分</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
</head>
 
<body>
<form action ="fairScore.php" method="post">
<span>请输入各个评委的打分,中间用空格隔开</span><br/><!--在输入时,每两个数之间只能有一个空格-->
<input type="text" name="score" value="<&#63;php echo $score;&#63;>" /><br/>
<input type="submit" value="提交计算平均成绩"/>
</form>
 
<&#63;php
fairScore($aScore);
&#63;>
</body>
<html>


2. Operation rendering

The above is the entire content of this article, I hope you all like it.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1041324.htmlTechArticleHow to use php to implement the judges' scorer, and php to implement the judges' scoring using the selection sort method. The first step is to select from the input array. Find the highest and lowest scores, then remove the highest and lowest scores...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn