練習集 L2-027 名人堂與代金券(25 分)(排序+排名)

對於在中國大學MOOC(http://www.icourse163.org/)學習“數據結構”課程的學生,想要獲得一張合格證書,總評成績必須達到 60 分及以上,並且有另加福利:總評分在 [G, 100] 區間內者,可以得到 50 元 PAT 代金券;在 [60, G) 區間內者,可以得到 20 元PAT代金券。全國考點通用,一年有效。同時任課老師還會把總評成績前 K 名的學生列入課程“名人堂”。本題就請你編寫程序,幫助老師列出名人堂的學生,並統計一共發出了面值多少元的 PAT 代金券。

輸入格式:

輸入在第一行給出 3 個整數,分別是 N(不超過 10 000 的正整數,爲學生總數)、G(在 (60,100) 區間內的整數,爲題面中描述的代金券等級分界線)、K(不超過 100 且不超過 N 的正整數,爲進入名人堂的最低名次)。接下來 N 行,每行給出一位學生的賬號(長度不超過15位、不帶空格的字符串)和總評成績(區間 [0, 100] 內的整數),其間以空格分隔。題目保證沒有重複的賬號。

輸出格式:

首先在一行中輸出發出的 PAT 代金券的總面值。然後按總評成績非升序輸出進入名人堂的學生的名次、賬號和成績,其間以 1 個空格分隔。需要注意的是:成績相同的學生享有並列的排名,排名並列時,按賬號的字母序升序輸出。

輸入樣例:

10 80 5
[email protected] 78
[email protected] 87
[email protected] 65
[email protected] 96
[email protected] 39
[email protected] 87
[email protected] 80
[email protected] 88
[email protected] 80
[email protected] 70

輸出樣例:

360
1 [email protected] 96
2 [email protected] 88
3 [email protected] 87
3 [email protected] 87
5 [email protected] 80
5 [email protected] 80

題解:還是排序題,注意分數相同排名也一樣。

代碼:

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct student{
    int id;
    char name[20];
    int grade;
}stu[10005];

bool cmp(student a,student b)
{
    if(a.grade!=b.grade)
        return a.grade>b.grade;
    return strcmp(a.name,b.name)<0;
}

int main()
{
    int n,g,k;
    scanf("%d%d%d",&n,&g,&k);
    for(int i=0;i<n;i++)
    {
        scanf("%s%d",stu[i].name,&stu[i].grade);
    }
    int cnta=0,cntb=0,money;
    for(int i=0;i<n;i++)
    {
        if(stu[i].grade>=g&&stu[i].grade<=100)
            cnta++;
        if(stu[i].grade>=60&&stu[i].grade<g)
            cntb++;
    }
    money=50*cnta+20*cntb;
    sort(stu,stu+n,cmp);
    //重點
    stu[0].id=1;
    for(int i=1;i<n;i++)
    {
        if(stu[i].grade==stu[i-1].grade)
            stu[i].id=stu[i-1].id;
        else
            stu[i].id=i+1;
    }

    printf("%d\n",money);
    int line=stu[k-1].grade;
    for(int i=0;i<n;i++)
    {
        if(stu[i].grade>=line)
        printf("%d %s %d\n",stu[i].id,stu[i].name,stu[i].grade);
    }
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章