dream.in.java

          能以不變應萬變是聰明人做事的準則。萬事從小事做起,積累小成功,問鼎大成功,是成功者的秘訣。

          得分排列

          背景:
          假設有一教師依學生座號輸入考試分數,現希望在輸入完畢後自動顯示學生分數的排行,當然學生的分數可能相同。

          實現思路:
          這個問題基本上要解不難,只要使用額外的一個排行陣列走訪分數陣列就可以了,直接使用下面的程式片段作說明:
          for(i = 0; i < count; i++) {
              juni[i] = 1;
              for(j = 0; j < count; j++) {
                  if(score[j] > score[i])
                      juni[i]++;
              }
          }

          printf("得分\t排行\n");
          for(i = 0; i < count; i++)
              printf("%d\t%d\n", score[i], juni[i]);

           
          上面這個方法雖然簡單,但是反覆計算的次數是n^2,如果n值變大,那麼運算的時間就會拖長;改變juni陣列的長度為n+2,並將初始值設定為0,如下所示:
          得分排行

          接下來走訪分數陣列,並在分數所對應的排行陣列索引元素上加1,如下所示:
          得分排行

          將排行陣列最右邊的元素設定為1,然後依序將右邊的元素值加至左邊一個元素,最後排行陣列中的「分數+1」」就是得該分數的排行,如下所示:
          得分排行
          這樣的方式看起來複雜,其實不過在計算某分數之前排行的人數,假設89分之前的排行人數為x人,則89分自然就是x+1了,這也是為什麼排行陣列最右邊要設定為1的原因;如果89分有y人,則88分自然就是x+y+1,整個陣列右邊元素向左加的原因正是如此。

          如果分數有負分的情況,由於C/C++或Java等程式語言無法處理負的索引,所以必須加上一個偏移值,將所有的分數先往右偏移一個範圍即可,最後顯示的時候記得減回偏移值就可以了。


          實現算法:
           1 package 算法.得分排序;
           2 
           3 import java.io.*;
           4 
           5 public class 得分排序 {
           6     public static void main(String[] args) throws NumberFormatException,
           7             IOException {
           8         final int MAX = 5;
           9         final int MIN = 0;
          10         int[] score = new int[MAX + 1];
          11         int[] juni = new int[MAX + 2];
          12         System.out.println();
          13         BufferedReader reader = new BufferedReader(new InputStreamReader(
          14                 System.in));
          15         int count = 0;
          16         do {
          17             System.out.print("輸入分數,-1結束:");
          18             score[count++= Integer.parseInt(reader.readLine());
          19         } while ((score[count - 1!= -1));
          20         count--;
          21         for(int i = 0; i< count; i++)
          22             juni[score[i]]++;//將分數的排行列陣置為1
          23         juni[MAX + 1= 1;//將元素下標為MAX+1的位置置為1,也即是第一名,之后不會再改變了
          24         for (int i = MAX; i >= MIN; i--)
          25             juni[i] = juni[i] + juni[i + 1];//排名陣列元素向左加
          26         System.out.println("得分\t排行");
          27         for (int i = 0; i < count; i++) {
          28             //score[i]的排名對應是juni[score[i] + 1]的值
          29             System.out.println(score[i] + "\t" + juni[score[i] + 1]);
          30         }
          31         /*
          32         for (int i = 0; i < count; i++) {
          33             System.out.print("score[" + i + "] = " + score[i] +"  "  );
          34         }
          35         System.out.println();
          36         for (int i = 0; i < count; i++) {
          37             System.out.print("jnui[" + i + "] = " + juni[i] +"  "  );
          38             
          39         }
          40         System.out.println(juni[5]+"  "+ juni[6]);
          41         */
          42     }
          43 }
          44 

          posted on 2009-04-12 15:45 YXY 閱讀(183) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 吕梁市| 福贡县| 舒城县| 芜湖县| 荣昌县| 临邑县| 大关县| 资溪县| 称多县| 平乐县| 富川| 雷州市| 红河县| 松阳县| 白朗县| 新晃| 文山县| 南丰县| 南宁市| 桂东县| 延庆县| 交城县| 凤城市| 西盟| 吴忠市| 清镇市| 锦州市| 汉中市| 龙南县| 盖州市| 聂拉木县| 高碑店市| 霞浦县| 洛宁县| 青川县| 利辛县| 武胜县| 喀喇| 缙云县| 社旗县| 龙川县|