scorerank.htm
来自「“常见程式演算”主要收集一些常见的程式练习题目」· HTM 代码 · 共 165 行
HTM
165 行
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<link rel="stylesheet" href="css/stdlayout.css" type="text/css">
<link rel="stylesheet" href="css/print.css" type="text/css">
<meta content="text/html; charset=gb2312" http-equiv="content-type">
<title>得分排行</title>
</head>
<body>
<h3><a href="http://caterpillar.onlyfun.net/GossipCN/index.html">From
Gossip@caterpillar</a></h3>
<h1><a href="AlgorithmGossip.htm">Algorithm Gossip: 得分排行</a></h1>
<h2>说明</h2>
假设有一教师依学生座号输入考试分数,现希望在输入完毕后自动显示学生分数的排行,当然学生的分数可能相同。<br>
<h2>解法</h2>
这个问题基本上要解不难,只要使用额外的一个排行阵列走访分数阵列就可以了,直接使用下面的程式片段作说明:<br>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">for(i = 0; i < count; i++) { </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> juni[i] = 1; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> for(j = 0; j < count; j++) { </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> if(score[j] > score[i]) </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> juni[i]++; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> } </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">} </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">printf("得分\t排行\n"); </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">for(i = 0; i < count; i++) </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> printf("%d\t%d\n", score[i], juni[i]); </span><br>
</div>
<br>
<br>
上面这个方法虽然简单,但是反覆计算的次数是n^2,如果n值变大,那么运算的时间就会拖长;改变juni阵列的长度为n+2,并将初始值设定为0,如下所示: <br>
<div style="text-align: center;"><img style="width: 418px; height: 61px;" alt="得分排行" title="得分排行" src="images/scoreRank-1.jpg"></div>
<br>
接下来走访分数阵列,并在分数所对应的排行阵列索引元素上加1,如下所示:<br>
<div style="text-align: center;"><img style="width: 418px; height: 64px;" alt="得分排行" title="得分排行" src="images/scoreRank-2.jpg"></div>
<br>
将排行阵列最右边的元素设定为1,然后依序将右边的元素值加至左边一个元素,最后排行阵列中的“分数+1””就是得该分数的排行,如下所示: <br>
<div style="text-align: center;"><img style="width: 482px; height: 157px;" alt="得分排行" title="得分排行" src="images/scoreRank-3.jpg"></div>
这样的方式看起来复杂,其实不过在计算某分数之前排行的人数,假设89分之前的排行人数为x人,则89分自然就是x+1了,这也是为什么排行阵列最右边要设定为1的原因;如果89分有y人,则88分自然就是x+y+1,整个阵列右边元素向左加的原因正是如此。<br>
<br>
如果分数有负分的情况,由于C/C++或Java等程式语言无法处理负的索引,所以必须加上一个偏移值,将所有的分数先往右偏移一个范围即可,最后显示的时候记得减回偏移值就可以了。 <br>
<br>
<h2> 实作</h2>
<ul>
<li> C </li>
</ul>
<pre>#include <stdio.h> <br>#include <stdlib.h> <br>#define MAX 100 <br>#define MIN 0 <br><br>int main(void) { <br> int score[MAX+1] = {0}; <br> int juni[MAX+2] = {0}; <br> int count = 0, i; <br><br> do { <br> printf("输入分数,-1结束:"); <br> scanf("%d", &score[count++]); <br> } while(score[count-1] != -1);<br><br> count--; <br><br> for(i = 0; i < count; i++) <br> juni[score[i]]++; <br> juni[MAX+1] = 1; <br><br> for(i = MAX; i >= MIN; i--) <br> juni[i] = juni[i] + juni[i+1]; <br><br> printf("得分\t排行\n"); <br> for(i = 0; i < count; i++) <br> printf("%d\t%d\n", score[i], juni[score[i]+1]); <br><br> return 0; <br>} <br></pre>
<br>
<ul>
<li> Java </li>
</ul>
<pre>import java.io.*;<br><br>public class ScoreRank {<br> public static void main(String[] args) <br> throws NumberFormatException, IOException {<br> final int MAX = 100;<br> final int MIN = 0;<br> <br> int[] score = new int[MAX+1]; <br> int[] juni = new int[MAX+2];<br> <br> BufferedReader reader = <br> new BufferedReader(<br> new InputStreamReader(System.in));<br> int count = 0; <br><br> do { <br> System.out.print("输入分数,-1结束:"); <br> score[count++] = <br> Integer.parseInt(reader.readLine()); <br> } while((score[count-1] != -1));<br><br> count--; <br><br> for(int i = 0; i < count; i++) <br> juni[score[i]]++; <br> juni[MAX+1] = 1; <br><br> for(int i = MAX; i >= MIN; i--) <br> juni[i] = juni[i] + juni[i+1]; <br><br> System.out.println("得分\t排行"); <br> for(int i = 0; i < count; i++) {<br> System.out.println(score[i] + "\t" + <br> juni[score[i]+1]); <br> }<br> }<br>} </pre>
<br>
</body>
</html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?