C语言利用sort函数快速排序(PAT真题- 1025. PAT Ranking)

发布于 / C语言 / 0 条评论

sort函数是C语言中一个用于快速排序的函数,可以免于写冒泡排序,二分查找等麻烦的东西,时间复杂度为n*log2(n)。

使用sort函数排序需要先引入头文件

#include <algorithm>

sort函数的原型:

sort(_RandomAccessIterator __first, _RandomAccessIterator __last,_Compare __comp)

其中第一个参数是排序起始点,第二个参数是排序终止点,第三个参数是用于排序的函数名。

这里以PAT甲级1025题(PAT Ranking)举例。下面是题干:

Programming Ability Test (PAT) is organized by the College of Computer Science and Technology of Zhejiang University. Each test is supposed to run simultaneously in several places, and the ranklists will be merged immediately after the test. Now it is your job to write a program to correctly merge all the ranklists and generate the final rank.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive number N (<=100), the number of test locations. Then N ranklists follow, each starts with a line containing a positive integer K (<=300), the number of testees, and then K lines containing the registration number (a 13-digit number) and the total score of each testee. All the numbers in a line are separated by a space.

Output Specification:

For each test case, first print in one line the total number of testees. Then print the final ranklist in the following format:

registration_number final_rank location_number local_rank

The locations are numbered from 1 to N. The output must be sorted in nondecreasing order of the final ranks. The testees with the same score must have the same rank, and the output must be sorted in nondecreasing order of their registration numbers.

Sample Input:

2
5
1234567890001 95
1234567890005 100
1234567890003 95
1234567890002 77
1234567890004 85
4
1234567890013 65
1234567890011 25
1234567890014 100
1234567890012 85

Sample Output:

9
1234567890005 1 1 1
1234567890014 1 2 1
1234567890001 3 1 2
1234567890003 3 1 2
1234567890004 5 1 4
1234567890012 5 2 2
1234567890002 7 1 5
1234567890013 8 2 3
1234567890011 9 2 4

大概意思就是告诉你有几个考场,然后分别告诉你考场的学生学号与成绩信息,要求对每个考场学生进行排序,再对全体学生进行排序。最后打印到屏幕上。

如果使用冒泡排序的话,程序会变得很麻烦,这里定义一个返回bool的函数:cmp,使用sort函数利用cmp函数对学生进行排序。代码如下:

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

using namespace std;

typedef struct _student{
  char id[13];  //考号 
  int score;  //成绩 
  int l_n;  //考场号 
  int l_rank;  //考场内排名 
} student; 

bool cmp(student a,student b) {
  if(a.score != b.score) return a.score>b.score;  //分数不等,按照分数排序,如果a分大于b分,返回真 
  else return strcmp(a.id,b.id) <0;  //分数相等,按考号排序。如果aid>=bid,strcmp为正,返回假,实现从小到大排序 
  //这里strcmp作用是,如果两个字符串相同,返回0,如果前者大于后者,返回正数。反之返回负数。
}

int main() {
  student stu[30010];  //定义学生成绩数组 
  int n, k, num = 0;  //这里num表示考生总数,n为考场数
  scanf("%d",&n);  //读取用户输入的考场数量
  for(int i = 1; i <= n; i++){  //循环[考场数]次,考场号为i(每轮循环读取考场人数与学生成绩 ) 
    scanf("%d", &k);  //读取考场人数 
    for(int j = 0; j < k; j++){  //循环[考场人数]次,每轮循环读取考生成绩 
      scanf("%s %d", stu[num].id, &stu[num].score);  //注意用%s可以读取一整个字符串,空格为止。存入id数组。id是char数组,本身就是地址,无需用"&"取地址 
      stu[num++].l_n = i;  //存入考场号,并将考生总数num+1 
    }
    sort(stu + num - k, stu + num, cmp);  //使用cmp函数对本次读取区间[num-k,num)的考生进行排序 
    stu[num- k].l_rank = 1;  //对本考场第一的考生的l_rank标记为1 
    for(int j = num - k + 1; j < num; j++){  //遍历剩余的考生 
      if(stu[j].score == stu[j - 1].score)  //如果第j名考生与下一名考生分数相同
        stu[j].l_rank = stu[j - 1].l_rank;  //并列 
      else
        stu[j].l_rank = j + 1 - (num-k);  //l_rank标记为下一个数字 
    }
  }
  sort(stu , stu + num , cmp);   //用cmp函数对所有考生排序 
  printf("%d\n",num);  //输出考生总数 
  int rank = 1;
  for(int i = 0; i < num; i++){
    if(i > 0 && stu[i].score != stu[i-1].score){
      rank = i + 1;
    }
    printf("%s %d %d %d\n",stu[i].id,rank,stu[i].l_n,stu[i].l_rank);
  }
  return 0;
}

编译运行。完美通过。

转载原创文章请注明,转载自: 斐斐のBlog » C语言利用sort函数快速排序(PAT真题- 1025. PAT Ranking)
目前还没有评论,快来抢沙发吧~