1028. List Sorting (25)

本文解析PAT-A-1028题目,使用C语言实现学生记录的排序功能。根据输入的学生记录(包括ID、姓名和成绩),按照指定列进行排序,并给出排序后的结果。代码使用qsort函数实现不同条件下的排序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原题地址:http://www.patest.cn/contests/pat-a-practise/1028

Excel can sort records according to any column. Now you are supposed to imitate this function.

Input

Each input file contains one test case. For each case, the first line contains two integers N (<=100000) and C, where N is the number of records and C is the column that you are supposed to sort the records with. Then N lines follow, each contains a record of a student. A student’s record consists of his or her distinct ID (a 6-digit number), name (a string with no more than 8 characters without space), and grade (an integer between 0 and 100, inclusive).

Output

For each test case, output the sorting result in N lines. That is, if C = 1 then the records must be sorted in increasing order according to ID’s; if C = 2 then the records must be sorted in non-decreasing order according to names; and if C = 3 then the records must be sorted in non-decreasing order according to grades. If there are several students who have the same name or grade, they must be sorted according to their ID’s in increasing order.

Sample Input 1
3 1
000007 James 85
000010 Amy 90
000001 Zoe 60
Sample Output 1
000001 Zoe 60
000007 James 85
000010 Amy 90
Sample Input 2
4 2
000007 James 85
000010 Amy 90
000001 Zoe 60
000002 James 98
Sample Output 2
000010 Amy 90
000002 James 98
000007 James 85
000001 Zoe 60
Sample Input 3
4 3
000007 James 85
000010 Amy 90
000001 Zoe 60
000002 James 90
Sample Output 3
000001 Zoe 60
000007 James 85
000002 James 90
000010 Amy 90

解题思路:考察STL中sort用法。但定睛一看我当时竟然是用qsort,还是太菜了

代码如下:

#include <stdio.h>
#include <algorithm>
#include <cstring>
#define MAXN 100010
typedef struct record{
    char id[8];
    char name[10];
    int grade;  
}rec;

rec st[MAXN];
int N,C;

int cmp1(const void* _a, const void* _b){
    rec a = *(rec*)_a;
    rec b = *(rec*)_b;
    return strcmp(a.id,b.id);
}

int cmp2(const void* _a, const void* _b){
    rec a = *(rec*)_a;
    rec b = *(rec*)_b;
    int t = strcmp(a.name,b.name);
    if(t == 0) return strcmp(a.id,b.id);
    else return t;
}

int cmp3(const void* _a, const void* _b){
    rec a = *(rec*)_a;
    rec b = *(rec*)_b;
    int t = a.grade - b.grade;
    if(t > 0) return 1;
    else if(t < 0) return -1;
    else return strcmp(a.id,b.id);
}

int main(){
    freopen("1028.txt","r",stdin);
    scanf("%d%d",&N,&C);
    for(int i = 0; i < N; i++){
        scanf("%s%s%d",&st[i].id,&st[i].name,&st[i].grade); 
    }
    switch(C){
        case 1: qsort(st,N,sizeof(st[0]),cmp1); break;
        case 2: qsort(st,N,sizeof(st[0]),cmp2); break;
        case 3: qsort(st,N,sizeof(st[0]),cmp3); break;  
    }
    for(int i = 0; i < N; i++){
        printf("%s %s %d\n",st[i].id,st[i].name,st[i].grade);   
    }
    return 0;
}
### Java 中 `Collections.sort` 的列表判空处理 在使用 `Collections.sort` 对列表进行排序之前,为了防止因操作空列表而引发异常(如 NullPointerException),可以先通过条件语句来判断列表是否为空或者长度是否为零。如果列表不为空,则执行排序逻辑;否则可以选择跳过排序或采取其他措施。 以下是实现这一功能的具体方法: #### 判空代码示例 ```java import java.util.*; public class SortExample { public static void main(String[] args) { List<Integer> list = new ArrayList<>(); // 添加数据到列表 (可选) // list.add(5); // list.add(2); // list.add(8); // 判断列表是否为空 if (list != null && !list.isEmpty()) { Collections.sort(list); // 默认升序排序 System.out.println("Sorted List: " + list); } else { System.out.println("List is empty or null. No sorting performed."); } } } ``` 上述代码中,在调用 `Collections.sort` 方法前进行了两步验证: 1. 验证列表对象本身是否为 `null`。 2. 使用 `isEmpty()` 方法确认列表是否有任何元素[^1]。 只有当这两项检查都满足时才会继续执行排序过程,从而有效避免潜在错误的发生。 #### 自定义比较器情况下的判空处理 对于带有自定义比较器的情况,同样需要遵循类似的判空流程。例如按照价格降序排列商品信息时: ```java import java.util.*; class Product implements Comparable<Product>{ private String name; private double price; public Product(String name, double price){ this.name=name; this.price=price; } @Override public int compareTo(Product otherProduct){ return Double.compare(otherProduct.getPrice(),this.getPrice()); } public String getName() { return name; } public double getPrice(){ return price; } @Override public String toString(){ return "[Name="+name+",Price="+price+"]"; } } public class CustomSort{ public static void main(String []args){ List<Product> productList=new ArrayList<>(); // 可以在此处添加产品实例 if(productList!=null && !productList.isEmpty()){ Collections.sort(productList,new Comparator<Product>() { @Override public int compare(Product p1, Product p2) { return Double.compare(p2.getPrice(),p1.getPrice()); // 降序 } }); for(Product product : productList){ System.out.println(product.toString()); } }else{ System.out.println("The product list is either null or empty!"); } } } ``` 此段程序展示了如何安全地应用带参数版本的 `Collections.sort` 函数完成复杂类型的排序任务,并且包含了必要的前置检验步骤。 #### 总结说明 无论是在简单场景还是涉及定制化规则的情况下运用 `Collections.sort` 进行数组整理工作时,都应该养成良好习惯——即事先做好充分的数据状态评估准备动作,比如检测目标集合是否存在以及其内部成员数量状况等基本信息,以此保障整个算法运行期间不会因为意外因素而导致崩溃等问题发生。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值