题解 | #成绩排序#

成绩排序

https://www.nowcoder.com/practice/3f27a0a5a59643a8abf0140b9a8cf1f7

#include <iostream>
#include <algorithm>
using namespace std;

int Partition(int A[], int B[], int low, int high) {
    int pivot = A[low];
    int pivot2 = B[low];

    while (low < high) {
        while (low < high && A[high] >= pivot)     high--;
        A[low] = A[high];
        B[low] = B[high];

        while (low < high && A[low] <= pivot)      low++;
        A[high] = A[low];
        B[high] = B[low];
    }
    A[low] = pivot;
    B[low] = pivot2;
    return low;
}

void Qsort(int A[], int B[], int low, int high) {
    if (low < high) {
        int pivotpos = Partition(A, B, low, high);
        Qsort(A, B, low, pivotpos - 1);
        Qsort(A, B, pivotpos + 1, high);
    }
}

void SameSort(int A[], int B[], int size) {
    int count = 1;
    int current = A[0];
    int start = 0;  //相同成绩区段

    for (int i = 1; i <= size; i++) {
        if (A[i] != current) {
            if (count != 1) sort(B + start, B + i);
            count = 1;
            start = i;
            current = A[i];
        } else count++;
    }
}

int main() {
    int size;  //学生人数
    while (scanf("%d", &size) != EOF) {
        int* sName = new int[size]();
        int* sGrade = new int[size]();
        for (int i = 0; i < size; i++)
            scanf("%d %d", &sName[i], &sGrade[i]);
        Qsort(sGrade, sName, 0, size - 1);
        SameSort(sGrade, sName, size);
        for (int i = 0; i < size; i++)
            printf("%d %d\n", sName[i], sGrade[i]);
        free(sName);
        free(sGrade);
    }
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
12-03 15:41
已编辑
淘天 算法工程师 31.0k*16.0
点赞 评论 收藏
分享
一个菜鸡罢了:哥们,感觉你的简历还是有点问题的,我提几点建议,看看能不能提供一点帮助 1. ”新余学院“别加粗,课程不清楚是否有必要写,感觉版面不如拿来写一下做过的事情,教育经历是你的弱势就尽量少写 2. “干部及社团经历”和“自我评价”删掉 3. 论文后面的“录用”和“小修”啥的都删掉,默认全录用,问了再说,反正小修毕业前肯定能发出来 4. 工作经验和研究成果没有体现你的个人贡献,着重包装一下个人贡献
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务