Table of Contents
如何使用php实现评委评分器,php实现评委评分
Home php教程 php手册 如何使用php实现评委评分器,php实现评委评分

如何使用php实现评委评分器,php实现评委评分

Jun 13, 2016 am 08:56 AM
php

如何使用php实现评委评分器,php实现评委评分

用选择排序法第一步从输入的数组中找到最高分和最低分,然后去掉一个最高分和一个最低分,得出选手的平均分。

1. 实现代码

<&#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>


Copy after login

2. 运行效果图

以上就是本文的全部内容,希望大家可以喜欢。

Statement of this Website
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

Hot Article

Hot Article

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles