题解 | #成绩排序#

成绩排序

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);
    }
}

全部评论

相关推荐

11-14 17:28
已编辑
门头沟学院 Java
点赞 评论 收藏
分享
菜菜咪:1. 可以使用简历网站的模版,美观度会更好一点 2. 邮箱可以重新申请一个,或者用qq邮箱的别名,部分hr可能会不喜欢数字邮箱 3. 项目经历最好分点描述,类似的项目很多,可以参考一下别人怎么写的 4. 自我评价可加可不加,技术岗更看重技术。最后,加油,优秀士兵
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务