Excel可以对一组纪录按任意指定列排序。现请编写程序实现类似功能。

输入格式:

输入的第一行包含两个正整数N(≤105) 和C,其中N是纪录的条数,C是指定排序的列号。之后有 N行,每行包含一条学生纪录。每条学生纪录由学号(6位数字,保证没有重复的学号)、姓名(不超过8位且不包含空格的字符串)、成绩([0, 100]内的整数)组成,相邻属性用1个空格隔开。

输出格式:

N行中输出按要求排序后的结果,即:当C=1时,按学号递增排序;当C=2时,按姓名的非递减字典序排序;当C=3时,按成绩的非递减排序。当若干学生具有相同姓名或者相同成绩时,则按他们的学号递增排序。

输入样例:

3 1
000007 James 85
000010 Amy 90
000001 Zoe 60

输出样例:

000001 Zoe 60
000007 James 85
000010 Amy 90



/*
  堆排序
*/
#include <bits/stdc++.h>
using namespace std;
typedef struct Node
{
    char name[10];
    char id[10];
    int score;
    Node operator = (Node ne)
    {
        strcpy(name, ne.name);
        strcpy(id, ne.id);
        score = ne.score;
    }
}Node;
Node Stu[101000];
int N, method;
void Input()
{
    for(int i = 1; i <= N; ++i)
      scanf("%s %s %d", Stu[i].id, Stu[i].name, &Stu[i].score);
}
bool cmp(Node stu1, Node stu2)
{
    if(method == 1)
    {
        if(strcmp(stu1.id, stu2.id) > 0)
            return true;
    }
    if(method == 2)
    {
        if(strcmp(stu1.name, stu2.name) > 0)
            return true;
        else if(strcmp(stu1.name, stu2.name) == 0)
        {
            if(strcmp(stu1.id, stu2.id) > 0)
              return true;
        }
    }
    if(method == 3)
    {
        if(stu1.score > stu2.score)
            return true;
        if(stu1.score == stu2.score)
            if(strcmp(stu1.id, stu2.id) > 0)
              return true;
    }
    return false;
}
void HeapAdjust(int s, int m)
{
    Stu[0] = Stu[s];
    for(int i = s * 2; i <= m; i *= 2)
    {
        if(i < m && cmp(Stu[i + 1], Stu[i]))
            i++;
        if(cmp(Stu[i], Stu[0]))
        {
            Stu[s] = Stu[i];
            s = i;
        }
        else
        {
            break;
        }
    }
    //printf("***%s %s %d %d\n", Stu[0].id, Stu[0].name, Stu[0].score, s);
    Stu[s] = Stu[0];
}
void HeapSort()
{
    for(int i = N / 2; i > 0; --i)
        HeapAdjust(i, N);
    for(int i = N; i > 1; --i)
    {
        Stu[0] = Stu[i];
        Stu[i] = Stu[1];
        Stu[1] = Stu[0];
        HeapAdjust(1, i - 1);
    }
}
int main()
{
  scanf("%d %d", &N, &method);
  Input();
  HeapSort();
  //printf("***********\n");
  for(int i = 1; i <= N; ++i)
      printf("%s %s %d\n", Stu[i].id, Stu[i].name, Stu[i].score);
}



阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6