zoukankan      html  css  js  c++  java
  • 基础实验7-2.4 PAT排名汇总 (25分)--结构体排序(快排)

     

     解题思路:每输入完一个考场考生信息后先按考场内排序+排名,最后做整体汇总排序+排名

    #include <stdio.h>
    #include <string.h>
    typedef struct {
        char ID[14];//考号
        int score;//成绩
        int rank;//总排名
        int Num;//考场号
        int rank1;//场内排名
    } Stu_Info;
    Stu_Info stu[30000];
    int cmp(Stu_Info a,Stu_Info b) {
        if(a.score==b.score)
            return strcmp(b.ID,a.ID);
        return a.score-b.score;
    }
    int Partition(Stu_Info stu[],int start,int end) {
        Stu_Info pivot=stu[start];
        int i=start,j=end;
        while(i<j) {
            while(i<j) {
                if(cmp(stu[j],pivot)>0) {
                    stu[i]=stu[j];
                    break;
                }
                j--;
            }
            while(i<j) {
                if(cmp(stu[i],pivot)<0) {
                    stu[j]=stu[i];
                    break;
                }
                i++;
            }
        }
        stu[i]=pivot;
        return i;
    }
    void sort(Stu_Info stu[],int start,int end) {
        if(start<end) {
            int t=Partition(stu,start,end);
            sort(stu,start,t-1);
            sort(stu,t+1,end);
        }
    }
    void rank(Stu_Info stu[],int start,int end,int type) {
        int i=start,cnt=1,pos=cnt;
        if(type==1) {
            stu[i].rank1=cnt;
            for(i=start+1; i<=end; i++) {
                pos++;
                if(stu[i].score!=stu[i-1].score)
                    cnt=pos;
                stu[i].rank1=cnt;
            }
        } else {
            stu[i].rank=cnt;
            for(i=start+1; i<=end; i++) {
                pos++;
                if(stu[i].score!=stu[i-1].score)
                    cnt=pos;
                stu[i].rank=cnt;
            }
        }
    
    }
    int main() {
        int n,j,pos,i,k=0,m;
        scanf("%d",&n);
        for(i=0; i<n; i++) {
            pos=k;
            scanf("%d",&m);
            for(j=0; j<m; j++) {
                scanf("%s %d",stu[k].ID,&stu[k].score);
                stu[k].Num=i+1;
                k++;
            }
            sort(stu,pos,k-1);
            rank(stu,pos,k-1,1);
        }
        sort(stu,0,k-1);
        rank(stu,0,k-1,0);
        printf("%d
    ",k);
        for(i=0; i<k; i++) {
            printf("%s %d %d %d
    ",stu[i].ID,stu[i].rank,stu[i].Num,stu[i].rank1);
        }
    }
    勤能补拙,熟能生巧
  • 相关阅读:
    字典生成式
    三元表达式
    迭代器
    装饰器
    闭包函数
    名称空间和作用域
    函数嵌套
    SQL Server 影响dbcc checkdb的 8 种因素
    SQL Server 对dbcc checkdb的优化
    SQL Server dbcc checkdb 修复
  • 原文地址:https://www.cnblogs.com/snzhong/p/12461777.html
Copyright © 2011-2022 走看看