題目1196:成績排序,1196:成績排序

來源:互聯網
上載者:User

題目1196:成績排序,1196:成績排序

題目描述:

用一維數組儲存學號和成績,然後,按成績排序輸出。

輸入:

輸入第一行包括一個整數N(1<=N<=100),代表學生的個數。
接下來的N行每行包括兩個整數p和q,分別代表每個學生的學號和成績。

輸出:

按照學生的成績從小到大進行排序,並將排序後的學生資訊列印出來。
如果學生的成績相同,則按照學號的大小進行從小到大排序。

範例輸入:
31 902 873 92
範例輸出:
2 871 903 92


C++代碼:

 #include<iostream>    #include<cstdio>    #include<cstdlib>    #include<cstring>    using namespace std;    const int maxn=100+3;    int num[maxn],grade[maxn];     int main()    {        int n;        while(scanf("%d",&n)!=EOF&&(n>=1&&n<=100))        {            for(int a=0;a<n;a++)                scanf("%d%d",&num[a],&grade[a]);            for(int i=n-1;i>=1;i--)            for(int j=0;j<i;j++)            if(grade[j]<grade[j+1]||(grade[j]==grade[j+1])&&(num[j]<num[j+1]))            {                int temp=grade[j];                grade[j]=grade[j+1];                grade[j+1]=temp;                temp=num[j];                num[j]=num[j+1];                num[j+1]=temp;            }            for(int b=n-1;b>=0;b--)            {                printf("%d %d\n",num[b],grade[b]);            }        }        return 0;    }/**************************************************************    Problem: 1196    User: Carvin    Language: C++    Result: Accepted    Time:50 ms    Memory:1520 kb****************************************************************/


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.