PAT Advanced 1025. PAT Ranking (25) (C語言實現(xiàn))

我的PAT系列文章更新重心已移至Github磷醋,歡迎來看PAT題解的小伙伴請到Github Pages瀏覽最新內(nèi)容(本篇文章鏈接)宗侦。此處文章目前已更新至與Github Pages同步。歡迎star我的repo德崭。

題目

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 ( \le 100 ), the number of test locations. Then N
ranklists follow, each starts with a line containing a positive integer K (
\le 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

思路

一道有關于排序的題目缘厢。題目要求得到每個考生分排名和總排名台谍,因此關鍵點就在于如何
排序镐依。

思路:由于讀取是以考點為組匹涮,因此可以在每讀完一個考點,即對此考點內(nèi)的考生進
行排序槐壳,得到分排名然低。最后再對整個列表進行排序,得到總排名务唐。

實現(xiàn)

使用一個數(shù)組存儲雳攘。排序時,使用qsort傳入不同位置和長度參數(shù)即可绍哎。

代碼

最新代碼@github来农,歡迎交流

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

/* registration number, location nubmer, total score, final rank, local rank */
typedef struct {
    char reg[14];
    int loc, score, rank, lrank;
} Testee, *pTestee;

int cmp(const void *a, const void *b)
{
    pTestee p1 = *(pTestee*)a, p2 = *(pTestee*)b;
    if(p1->score != p2->score)
        return p2->score - p1->score;
    else
        return strcmp(p1->reg, p2->reg);
}

int main()
{
    int N, K;
    Testee buffers[100 * 300] = {0}, *q = buffers;
    pTestee testees[100 * 300] = {0}, *p = testees;

    scanf("%d", &N);
    for(int n = 0; n < N; n++)
    {
        /* read one test location */
        scanf("%d", &K);
        for(int k = 0; k < K; k++, p++)
        {
            *p = q++;  /* use a new cell */
            (*p)->loc = n + 1;
            scanf("%s %d", (*p)->reg, &(*p)->score);
        }

        /* sort the kth location sublist */
        qsort(p - K, K, sizeof(pTestee), cmp);

        /* get the local rank, i is the offset (we need left K cells of p) */
        int lrank = 1;
        pTestee *lp;
        (*(p - K))->lrank = lrank;  /* boundary special case */
        for(int i = 1; i < K; i++)
        {
            lp = p - K + i;
            if((*lp)->score != (*(lp - 1))->score)
                lrank = i + 1;
            (*lp)->lrank = lrank;
        }
    }

    /* sort the whole list */
    qsort(testees, p - testees, sizeof(pTestee), cmp);
    /* get the final rank */
    int rank = 1;
    pTestee *fp;
    (*testees)->rank = rank;
    for(int i = 1; testees + i < p; i++)
    {
        fp = testees + i;
        if((*fp)->score != (*(fp - 1))->score)
            rank = i + 1;
        (*fp)->rank = rank;
    }

    /* output */
    printf("%ld\n", p - testees);
    for(pTestee* i = testees; i < p; i++)
        printf("%s %d %d %d\n", (*i)->reg, (*i)->rank, (*i)->loc, (*i)->lrank);

    return 0;
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末鞋真,一起剝皮案震驚了整個濱河市崇堰,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌涩咖,老刑警劉巖海诲,帶你破解...
    沈念sama閱讀 221,198評論 6 514
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異檩互,居然都是意外死亡特幔,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,334評論 3 398
  • 文/潘曉璐 我一進店門闸昨,熙熙樓的掌柜王于貴愁眉苦臉地迎上來蚯斯,“玉大人薄风,你說我怎么就攤上這事∨那叮” “怎么了遭赂?”我有些...
    開封第一講書人閱讀 167,643評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長横辆。 經(jīng)常有香客問我撇他,道長,這世上最難降的妖魔是什么狈蚤? 我笑而不...
    開封第一講書人閱讀 59,495評論 1 296
  • 正文 為了忘掉前任困肩,我火速辦了婚禮,結(jié)果婚禮上脆侮,老公的妹妹穿的比我還像新娘锌畸。我一直安慰自己,他們只是感情好他嚷,可當我...
    茶點故事閱讀 68,502評論 6 397
  • 文/花漫 我一把揭開白布蹋绽。 她就那樣靜靜地躺著,像睡著了一般筋蓖。 火紅的嫁衣襯著肌膚如雪卸耘。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,156評論 1 308
  • 那天粘咖,我揣著相機與錄音蚣抗,去河邊找鬼。 笑死瓮下,一個胖子當著我的面吹牛翰铡,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播讽坏,決...
    沈念sama閱讀 40,743評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼锭魔,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了路呜?” 一聲冷哼從身側(cè)響起迷捧,我...
    開封第一講書人閱讀 39,659評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎胀葱,沒想到半個月后漠秋,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,200評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡抵屿,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,282評論 3 340
  • 正文 我和宋清朗相戀三年庆锦,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片轧葛。...
    茶點故事閱讀 40,424評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡搂抒,死狀恐怖艇搀,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情求晶,我是刑警寧澤中符,帶...
    沈念sama閱讀 36,107評論 5 349
  • 正文 年R本政府宣布,位于F島的核電站誉帅,受9級特大地震影響淀散,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜蚜锨,卻給世界環(huán)境...
    茶點故事閱讀 41,789評論 3 333
  • 文/蒙蒙 一档插、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧亚再,春花似錦郭膛、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,264評論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至如捅,卻和暖如春棍现,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背镜遣。 一陣腳步聲響...
    開封第一講書人閱讀 33,390評論 1 271
  • 我被黑心中介騙來泰國打工己肮, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人悲关。 一個月前我還...
    沈念sama閱讀 48,798評論 3 376
  • 正文 我出身青樓谎僻,卻偏偏與公主長得像,于是被迫代替她去往敵國和親寓辱。 傳聞我的和親對象是個殘疾皇子艘绍,可洞房花燭夜當晚...
    茶點故事閱讀 45,435評論 2 359

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