C++程式--輸入考試分數後再排序


動機:
對於輸入學生考試分數完畢後,
能夠顯示學生分數的排名,
要如何寫個C++程式呢?!

說明:
利用 Code::Blocks 10.05 免費軟體,
再撰寫相關的程式即可達成要求(至於演算法或邏輯條件可能因需求而有所不同),
程式碼如下:



#include <stdio.h>
#include <stdlib.h>

#define MAX 100
#define MIN 0

using namespace std;

// duplicate output char
void printchar(char c, int n)
{
    int i;
    for(i = 1; i <= n; i++) printf("%c", c);
}

int main(void)
{
    int score[MAX + 1] = {0};
    int juni[MAX + 2] = {0};
    int count = 0;

    do
    {
        printf("請輸入[分數]再按[Enter],若按[-1]則結束:");
        scanf("%d", &score[count++]);
    }
    while(score[count - 1] != -1);

    count--;

    int i, j;
    for(i = 0; i < count; i++)
        juni[score[i]]++;

    juni[MAX + 1] = 1;

    for(i = MAX; i >= MIN; i--)
        juni[i] = juni[i] + juni[i + 1];

    if (score[0] != -1)
    {
        printf("\n\n得分\t排行\n");
        printf("====\t====\n");
    }

    // 按照排名順序印出分數
    for(i = 1; i <= count; i++)
    {
        for (j = 0; j < count; j++)
        {
            if (i == juni[score[j] + 1])
            {
                printf("%4d\t%4d\n", score[j], juni[score[j] + 1 ]);
                break;
            }
        }
    }

    if (score[0] != -1)
    {
        printchar('=', 12);
        printf("\n總共輸入 %d 筆分數!!\n\n", count);
    }

    system("pause");
    return 0;
}



參攷: Algorithm Gossip: 得分排行

執行檔下載連結: https://docs.google.com/open?id=0B_4eUrknq7N1OGZmZGE1NTMtOGZkOC00ZGE4LThmYTEtMmU2N2UwMjEyYzkx

留言

Unknown寫道…
您好!我是阿助
我在您之前的部落格有看到一篇是關於要在tomcat上安裝php的文章,因為我本身想在tomcat上運行wordpress所以找到您的文章~然後我有照您的步驟做了,但是還是出現錯誤404,不知道是出了甚麼原因,或者是檔案有問題之類的~希望您可以不吝嗇地提供意見,謝謝
^_^