1025 PAT Ranking (25分)

Programming Ability Test (PAT) is organized by the College of Computer Science and Technology of Zhejiang University. Each test is supposed to run simultaneously in several places, and the ranklists will be merged immediately after the test. Now it is your job to write a program to correctly merge all the ranklists and generate the final rank.

Input Specification:
Each input file contains one test case. For each case, the first line contains a positive number N (≤100), the number of test locations. Then N ranklists follow, each starts with a line containing a positive integer K (≤300), the number of testees, and then K lines containing the registration number (a 13-digit number) and the total score of each testee. All the numbers in a line are separated by a space.

Output Specification:
For each test case, first print in one line the total number of testees. Then print the final ranklist in the following format:

registration_number final_rank location_number local_rank
The locations are numbered from 1 to N. The output must be sorted in nondecreasing order of the final ranks. The testees with the same score must have the same rank, and the output must be sorted in nondecreasing order of their registration numbers.

Sample Input:
2
5
1234567890001 95
1234567890005 100
1234567890003 95
1234567890002 77
1234567890004 85
4
1234567890013 65
1234567890011 25
1234567890014 100
1234567890012 85
Sample Output:
9
1234567890005 1 1 1
1234567890014 1 2 1
1234567890001 3 1 2
1234567890003 3 1 2
1234567890004 5 1 4
1234567890012 5 2 2
1234567890002 7 1 5
1234567890013 8 2 3
1234567890011 9 2 4

分析:
題目要求先輸入考場個數(shù)驱负,再輸入每個考場人數(shù)和各個考場的考生分?jǐn)?shù)猪贪。最終輸出所有學(xué)生的注冊號、總排名汉矿、考場號愕乎、考場排名亮蒋。
按考場讀入學(xué)生信息后,立即計算考場排名妆毕。再對所有學(xué)生進(jìn)行排序慎玖,輸出最終結(jié)果。

C++:

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int maxn = 30010;

struct Student {
    char id[15];
    int score, loc, locRank, finalRank;//id,分?jǐn)?shù)笛粘,考場號趁怔,考場排名,總排名
} stus[maxn];

bool cmp(Student a, Student b) {
    if (a.score != b.score) {
        return a.score > b.score;
    } else {
        return strcmp(a.id, b.id) < 0;
    }
}

int main() {
    int n, k, num = 0;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        scanf("%d", &k);
        for (int j = 0; j < k; j++) {
            scanf("%s %d", &stus[num].id, &stus[num].score);
            stus[num].loc = i;
            num++;
        }
        //考場內(nèi)排名
        sort(stus + num - k, stus + num, cmp);
        stus[num - k].locRank = 1;
        for (int j = num - k + 1; j < num; j++) {
            if (stus[j].score == stus[j - 1].score) {
                stus[j].locRank = stus[j - 1].locRank;
            } else {
                stus[j].locRank = j + 1 - (num - k);
            }
        }
    }
    sort(stus, stus + num, cmp);
    stus[0].finalRank = 1;
    for (int i = 1; i < num; i++) {
        if (stus[i].score == stus[i - 1].score) {
            stus[i].finalRank = stus[i - 1].finalRank;
        } else {
            stus[i].finalRank = i + 1;
        }
    }
    printf("%d\n", num);
    for (int i = 0; i < num; i++) {
        printf("%s %d %d %d\n", stus[i].id, stus[i].finalRank, stus[i].loc, stus[i].locRank);
    }
    return 0;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末薪前,一起剝皮案震驚了整個濱河市润努,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌示括,老刑警劉巖铺浇,帶你破解...
    沈念sama閱讀 219,427評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異垛膝,居然都是意外死亡鳍侣,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,551評論 3 395
  • 文/潘曉璐 我一進(jìn)店門吼拥,熙熙樓的掌柜王于貴愁眉苦臉地迎上來倚聚,“玉大人,你說我怎么就攤上這事凿可』笳郏” “怎么了?”我有些...
    開封第一講書人閱讀 165,747評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長惨驶。 經(jīng)常有香客問我白热,道長,這世上最難降的妖魔是什么粗卜? 我笑而不...
    開封第一講書人閱讀 58,939評論 1 295
  • 正文 為了忘掉前任屋确,我火速辦了婚禮,結(jié)果婚禮上休建,老公的妹妹穿的比我還像新娘。我一直安慰自己评疗,他們只是感情好测砂,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,955評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著百匆,像睡著了一般砌些。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上加匈,一...
    開封第一講書人閱讀 51,737評論 1 305
  • 那天存璃,我揣著相機(jī)與錄音,去河邊找鬼雕拼。 笑死纵东,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的啥寇。 我是一名探鬼主播偎球,決...
    沈念sama閱讀 40,448評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼辑甜!你這毒婦竟也來了衰絮?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,352評論 0 276
  • 序言:老撾萬榮一對情侶失蹤磷醋,失蹤者是張志新(化名)和其女友劉穎猫牡,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體邓线,經(jīng)...
    沈念sama閱讀 45,834評論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡淌友,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,992評論 3 338
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了骇陈。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片亩进。...
    茶點(diǎn)故事閱讀 40,133評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖缩歪,靈堂內(nèi)的尸體忽然破棺而出归薛,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 35,815評論 5 346
  • 正文 年R本政府宣布主籍,位于F島的核電站习贫,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏千元。R本人自食惡果不足惜苫昌,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,477評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望幸海。 院中可真熱鬧祟身,春花似錦、人聲如沸物独。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,022評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽挡篓。三九已至婉陷,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間官研,已是汗流浹背秽澳。 一陣腳步聲響...
    開封第一講書人閱讀 33,147評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留戏羽,地道東北人担神。 一個月前我還...
    沈念sama閱讀 48,398評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像始花,于是被迫代替她去往敵國和親杏瞻。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,077評論 2 355

推薦閱讀更多精彩內(nèi)容