PAT Advanced 1028. List Sorting (25) (C語言實現(xiàn))

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

題目

Excel can sort records according to any column. Now you are supposed to
imitate this function.

Input Specification:

Each input file contains one test case. For each case, the first line contains
two integers N ( \le 10^5 ) and C , where N is the number of records
and C is the column that you are supposed to sort the records with. Then N
lines follow, each contains a record of a student. A student's record consists
of his or her distinct ID (a 6-digit number), name (a string with no more than
8 characters without space), and grade (an integer between 0 and 100,
inclusive).

Output Specification:

For each test case, output the sorting result in N lines. That is, if C =
1 then the records must be sorted in increasing order according to ID's; if
C = 2 then the records must be sorted in non-decreasing order according to
names; and if C = 3 then the records must be sorted in non-decreasing order
according to grades. If there are several students who have the same name or
grade, they must be sorted according to their ID's in increasing order.

Sample Input 1:

3 1
000007 James 85
000010 Amy 90
000001 Zoe 60

Sample Output 1:

000001 Zoe 60
000007 James 85
000010 Amy 90

Sample Input 2:

4 2
000007 James 85
000010 Amy 90
000001 Zoe 60
000002 James 98

Sample Output 2:

000010 Amy 90
000002 James 98
000007 James 85
000001 Zoe 60

Sample Input 3:

4 3
000007 James 85
000010 Amy 90
000001 Zoe 60
000002 James 90

Sample Output 3:

000001 Zoe 60
000007 James 85
000002 James 90
000010 Amy 90

思路

應(yīng)該也是很基本的排序焰络。三種排序選項,后兩個還有二級排序符喝。也沒有什么特殊的處理闪彼,所以很簡單。

代碼

最新代碼@github协饲,歡迎交流

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

typedef struct {
    int ID;
    char name[9];
    int grade;
} Student, *pStudent;

/* increasing by ID */
int cmp1(const void *a, const void *b)
{
    pStudent s1 = *(pStudent*)a, s2 = *(pStudent*)b;
    return s1->ID - s2->ID;
}

/* non decreasing by name, then increasing by ID */
int cmp2(const void *a, const void *b)
{
    pStudent s1 = *(pStudent*)a, s2 = *(pStudent*)b;
    if(strcmp(s1->name, s2->name))
        return strcmp(s1->name, s2->name);
    else
        return s1->ID - s2->ID;
}

/* non decreasing by grade, then increasing by ID */
int cmp3(const void *a, const void *b)
{
    pStudent s1 = *(pStudent*)a, s2 = *(pStudent*)b;
    if(s1->grade - s2->grade)
        return s1->grade - s2->grade;
    else
        return s1->ID - s2->ID;
}

int main()
{
    int N, C;
    Student buffer[100000] = {0};
    pStudent students[100000] = {0}, *s = students;

    scanf("%d %d", &N, &C);
    for(int i = 0; i < N; i++, s++)
    {
        /* use a new struct buffer */
        *s = buffer + i;
        scanf("%d %s %d", &(*s)->ID, (*s)->name, &(*s)->grade);
    }

    if(C == 1)  /* increasing ID */
        qsort(students, N, sizeof(pStudent), cmp1);
    else if(C == 2)  /* non-decreasing name */
        qsort(students, N, sizeof(pStudent), cmp2);
    else  /* C == 3, non-decreasing grade */
        qsort(students, N, sizeof(pStudent), cmp3);

    for(int i = 0; i < N; i++)
        printf("%06d %s %d\n", students[i]->ID,
                               students[i]->name,
                               students[i]->grade);

    return 0;
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末畏腕,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子茉稠,更是在濱河造成了極大的恐慌描馅,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,591評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件而线,死亡現(xiàn)場離奇詭異铭污,居然都是意外死亡恋日,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,448評論 3 392
  • 文/潘曉璐 我一進店門况凉,熙熙樓的掌柜王于貴愁眉苦臉地迎上來谚鄙,“玉大人各拷,你說我怎么就攤上這事刁绒。” “怎么了烤黍?”我有些...
    開封第一講書人閱讀 162,823評論 0 353
  • 文/不壞的土叔 我叫張陵知市,是天一觀的道長。 經(jīng)常有香客問我速蕊,道長嫂丙,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,204評論 1 292
  • 正文 為了忘掉前任规哲,我火速辦了婚禮跟啤,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘唉锌。我一直安慰自己隅肥,他們只是感情好,可當我...
    茶點故事閱讀 67,228評論 6 388
  • 文/花漫 我一把揭開白布袄简。 她就那樣靜靜地躺著腥放,像睡著了一般。 火紅的嫁衣襯著肌膚如雪绿语。 梳的紋絲不亂的頭發(fā)上秃症,一...
    開封第一講書人閱讀 51,190評論 1 299
  • 那天,我揣著相機與錄音吕粹,去河邊找鬼种柑。 笑死,一個胖子當著我的面吹牛匹耕,可吹牛的內(nèi)容都是我干的聚请。 我是一名探鬼主播,決...
    沈念sama閱讀 40,078評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼泌神,長吁一口氣:“原來是場噩夢啊……” “哼良漱!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起欢际,我...
    開封第一講書人閱讀 38,923評論 0 274
  • 序言:老撾萬榮一對情侶失蹤母市,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后损趋,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體患久,經(jīng)...
    沈念sama閱讀 45,334評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,550評論 2 333
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了蒋失。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片返帕。...
    茶點故事閱讀 39,727評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖篙挽,靈堂內(nèi)的尸體忽然破棺而出荆萤,到底是詐尸還是另有隱情,我是刑警寧澤铣卡,帶...
    沈念sama閱讀 35,428評論 5 343
  • 正文 年R本政府宣布链韭,位于F島的核電站,受9級特大地震影響煮落,放射性物質(zhì)發(fā)生泄漏敞峭。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,022評論 3 326
  • 文/蒙蒙 一蝉仇、第九天 我趴在偏房一處隱蔽的房頂上張望旋讹。 院中可真熱鬧,春花似錦轿衔、人聲如沸沉迹。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,672評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽胚股。三九已至,卻和暖如春裙秋,著一層夾襖步出監(jiān)牢的瞬間琅拌,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,826評論 1 269
  • 我被黑心中介騙來泰國打工摘刑, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留进宝,地道東北人。 一個月前我還...
    沈念sama閱讀 47,734評論 2 368
  • 正文 我出身青樓枷恕,卻偏偏與公主長得像党晋,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子徐块,可洞房花燭夜當晚...
    茶點故事閱讀 44,619評論 2 354

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