ACM零起點2017-7-25(sort對結構體排序 PK 自創C語言對結構體快排)__C語言

來源:互聯網
上載者:User

題目來源:https://vjudge.net


H - 結構體排序  


Excel可以對一組紀錄按任意指定列排序。現請你編寫程式實作類別似功能。 Input

測試輸入包含若干測試案例。每個測試案例的第1行包含兩個整數 N (<=100000) 和 C,其中 N 是紀錄的條數,C 是指定排序的列號。以下有 N 
行,每行包含一條學生紀錄。每條學生紀錄由學號(6位元字,同組測試中沒有重複的學號)、姓名(不超過8位且不包含空格的字串)、成績(閉區間 0,1000,100內的整數)組成,每個項目間用1個空格隔開。當讀到 N=0 時,全部輸入結束,相應的結果不要輸出。

Output對每個測試案例,首先輸出1行“Case i:”,其中 i 是測試案例的編號(從1開始)。隨後在 N 行中輸出按要求排序後的結果,即:當 C=1 時,按學號遞增排序;當 C=2時,按姓名的非遞減字典序排序;當 C=3 
時,按成績的非遞減排序。當若干學生具有相同姓名或者相同成績時,則按他們的學號遞增排序。


Sample Input

3 1000007 James 85000010 Amy 90000001 Zoe 604 2000007 James 85000010 Amy 90000001 Zoe 60000002 James 984 3000007 James 85000010 Amy 90000001 Zoe 60000002 James 900 0
Sample Output
Case 1:000001 Zoe 60000007 James 85000010 Amy 90Case 2:000010 Amy 90000002 James 98000007 James 85000001 Zoe 60Case 3:000001 Zoe 60000007 James 85000002 James 90000010 Amy 90

先掛上AC的代碼:(為了練習C語言版的快排,寫成如此冗長的代碼,昨天學習了sort對結構體排序,如何用sort對新結構體進行排序是關鍵,具體知識點和代碼往後看)


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


//FILE *fp;


typedef struct
{
    char no[20];
    char name[20];
    int score;
}stu;


stu student[100002];


void _swap(int *a,int *b)
{
    int t;
    t=*a;
    *a=*b;
    *b=t;
}


void _swapstr(char s1[],char s2[])
{
    char temp[1000];
    strcpy(temp,s1);
    strcpy(s1,s2);
    strcpy(s2,temp);
}


void quick_sort_no(stu a[],int s,int t)
{
    int i,j;
    i=s;
    j=t+1;
    if(s<t)
    {
        while(1)
        {
            do{i++;}while(!(strcmp(a[i].no,a[s].no)>0 || i==t));
            do{j--;}while(!(strcmp(a[j].no,a[s].no)<0 || j==s));
            if(i<j)
            {
                _swapstr(a[i].no,a[j].no);
                _swapstr(a[i].name,a[j].name);
                _swap(&a[i].score,&a[j].score);
            }


            else
                break;
        }
        _swapstr(a[s].no,a[j].no);
        _swapstr(a[s].name,a[j].name);
        _swap(&a[s].score,&a[j].score);
        quick_sort_no(a,s,j-1);
        quick_sort_no(a,j+1,t);
    }
}


void quick_sort_name(stu a[],int s,int t)
{
    int i,j;
    i=s;
    j=t+1;
    if(s<t)
    {
        while(1)
        {
            do{i++;}while(!(strcmp(a[i].name,a[s].name)>0 || (strcmp(a[i].name,a[s].name)==0 && strcmp(a[i].no,a[s].no)>0) || i==t));
            do{j--;}while(!(strcmp(a[j].name,a[s].name)<0 || (strcmp(a[j].name,a[s].name)==0 && strcmp(a[j].no,a[s].no)<0) || j==s));
            if(i<j)
            {
                _swapstr(a[i].no,a[j].no);
                _swapstr(a[i].name,a[j].name);
                _swap(&a[i].score,&a[j].score);
            }
            else
                break;
        }
        _swapstr(a[s].no,a[j].no);
        _swapstr(a[s].name,a[j].name);
        _swap(&a[s].score,&a[j].score);
        quick_sort_name(a,s,j-1);
        quick_sort_name(a,j+1,t);
    }
}




void quick_sort_score(stu a[],int s,int t)
{
    int i,j;
    i=s;
    j=t+1;
    if(s<t)
    {
        while(1)
        {
            do{i++;}while(!(a[i].score>a[s].score || (a[i].score==a[s].score && strcmp(a[i].no,a[s].no)>0) || i==t));
            do{j--;}while(!(a[j].score<a[s].score || (a[j].score==a[s].score && strcmp(a[j].no,a[s].no)<0) || j==s));
            if(i<j)
            {
                _swapstr(a[i].no,a[j].no);
                _swapstr(a[i].name,a[j].name);
                _swap(&a[i].score,&a[j].score);
            }


            else
                break;
        }
        _swapstr(a[s].no,a[j].no);
        _swapstr(a[s].name,a[j].name);
        _swap(&a[s].score,&a[j].score);
        quick_sort_score(a,s,j-1);
        quick_sort_score(a,j+1,t);
    }
}


int main()
{
    //fp=fopen("input.txt","r");
    int n,c;
    int num=1;
    while(scanf("%d%d",&n,&c))
    {
        if(n==0 && c==0)
            break;
        for(int i=0;i<n;i++)
        {
            scanf("%s%s%d",student[i].no,student[i].name,&student[i].score);
            //fscanf(fp,"%s%s%d",student[i].no,student[i].name,&student[i].score);
        }
        switch(c)
        {
        case 1:
            quick_sort_no(student,0,n-1);
            printf("Case %d:\n",num++);
            for(int i=0;i<n;i++)
            {
                printf("%s %s %d\n",student[i].no,student[i].name,student[i].score);
            }
            break;
        case 2:
            quick_sort_name(student,0,n-1);
            /*int i,j,k;
            char t[1000];
            for(i=0;i<n;i++)
            {
                k=i;
                for(j=i+1;j<n;j++)
                {
                    if(strcmp(student[j].name,student[k].name)<0 || (strcmp(student[j].name,student[k].name)==0 && strcmp(student[j].no,student[k].no)<0))
                    {
                        _swapstr(student[j].name,student[k].name);
                        _swapstr(student[j].no,student[k].no);
                        _swap(&student[j].score,&student[k].score);
                    }
                }
            }*/
            printf("Case %d:\n",num++);
            for(int i=0;i<n;i++)
            {
                printf("%s %s %d\n",student[i].no,student[i].name,student[i].score);
            }
            break;
        case 3:
            quick_sort_score(student,0,n-1);
            printf("Case %d:\n",num++);
            for(int i=0;i<n;i++)
            {
                printf("%s %s %d\n",student[i].no,student[i].name,student[i].score);
            }
            break;
        }
    }
    return 0;
}


sort實現對結構體排序的方法如下(此處以從大到小為例):

對結構體中的字串寫cmp函數也是類似的哦。詳見下面代碼


typedef struct
{
    int width;
    int len;
}rect;


bool cmp(rect x,rect y)
{
    if(x.len!=y.len)
        return x.len>y.len;
    return x.width>y.width;
}


AC代碼:


#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;


//FILE*fp;


typedef struct
{
    char no[10];
    char name[20];
    int score;
}student;


student stu[100005];


bool no_cmp(student s1,student s2)
{
    return strcmp(s1.no,s2.no)<0;
}


bool name_cmp(student s1,student s2)
{
    if(strcmp(s1.name,s2.name)!=0)
        return strcmp(s1.name,s2.name)<0;
    return strcmp(s1.no,s2.no)<0;
}


bool score_cmp(student s1,student s2)
{
    if(s1.score!=s2.score)
        return s1.score<s2.score;
    return strcmp(s1.no,s2.no)<0;
}


int main()
{
    //fp=fopen("input.txt","r");
    int m,n;
    int num=0;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        if(m==0 && n==0)break;
        for(int i=0;i<n;i++)
            scanf("%s%s%d",stu[i].no,stu[i].name,&stu[i].score);


        switch(m)
        {
        case 1:
            sort(stu,stu+n,no_cmp);break;
        case 2:
            sort(stu,stu+n,name_cmp);break;
        case 3:
            sort(stu,stu+n,score_cmp);break;
        }
        printf("Case %d:\n",++num);
        for(int i=0;i<n;i++)
            printf("%s %s %d\n",stu[i].no,stu[i].name,stu[i].score);
    }
    return 0;
}



上面代碼,注意switch中的case與break的搭配使用,小錯誤讓我調試許久,鬧心……


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.