PAT甲级真题 1028 List Sorting (25分) C++实现(简单排序)

news/2024/5/19 23:36:51 标签: 数据结构, 快速排序, 算法

题目

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

思路

建立结构体数组,为每个指定的列定义比较函数对数组排序即可。

代码

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

struct Stu{
    string id;
    string name;
    int grade;
};
bool cmp1(Stu s1, Stu s2){
    return s1.id < s2.id;
}
bool cmp2(Stu s1, Stu s2){
    return s1.name==s2.name ? s1.id<s2.id : s1.name<s2.name;
}
bool cmp3(Stu s1, Stu s2){
    return s1.grade==s2.grade ? s1.id<s2.id : s1.grade<s2.grade;
}
int main(){
    int n, col;
    cin >> n >> col;
    vector<Stu> S(n);
    for (int i=0; i<n; i++){
        cin >> S[i].id >> S[i].name >> S[i].grade; 
    }

    if (col==1) {
        sort(S.begin(), S.end(), cmp1);
    }
    else if (col==2) {
        sort(S.begin(), S.end(), cmp2);
    }
    else if (col==3) {
        sort(S.begin(), S.end(), cmp3);
    }

    for (int i=0; i<n; i++){
        cout << S[i].id << " " << S[i].name << " " << S[i].grade << endl;
    }
    return 0;
}


http://www.niftyadmin.cn/n/905928.html

相关文章

hprof是什么文件java_hprof是什么文件

hprof是什么文件&#xff1f;是java进程的内存镜像文件&#xff0c;里面包含了内存堆详细的使用信息。最近学习深入java虚拟机的书&#xff0c;照着里面的例子跑了下。如何打开hprof&#xff1f;下面是demo&#xff1a;/*** VM Args&#xff1a;-Xms20m -Xmx20m -XX:HeapDumpOn…

【整理分享2】html colspan属性的定义及使用方法介绍

html colspan属性的定义是什么&#xff1f;html colspan属性的使用方法介绍,现在介绍本篇文章&#xff0c;主要讲述了关于HTML td和html th两个标签的colspan属性的定义和具体的使用方法&#xff08;附实例&#xff09; HTML<td>标签的colspan属性的定义和用法&#xff1…

PAT甲级真题 1029 Median (25分) C++实现(归并排序思想,两个有序数组找中位数)

题目 Given an increasing sequence S of N integers, the median is the number at the middle position. For example, the median of S1 { 11, 12, 13, 14 } is 12, and the median of S2 { 9, 10, 15, 16, 17 } is 15. The median of two sequences is defined to be the…

洋灰三角(矩阵快速幂的两种解法)

链接&#xff1a;https://www.nowcoder.com/acm/contest/136/J来源&#xff1a;牛客网 洋灰是一种建筑材料&#xff0c;常用来筑桥搭建高层建筑&#xff0c;又称&#xff0c;水泥、混凝土。 WHZ有很多铸造成三角形的洋灰块&#xff0c;他想把这些洋灰三角按照一定的规律放到摆成…

java jni 调用c函数_简单JNI的使用--在Java中调用C库函数

在Android Framework中&#xff0c;需要提供一种媒介或桥梁&#xff0c;将Java层(上层)与C/C(底层)有机地联系起来&#xff0c;使得它们相互协调&#xff0c;共同完成某些任务。在这两层之间充当连接桥梁这一角色的就是Java本地接口(JNI,Java Native Interface)&#xff0c;它允…

PAT甲级真题 1030 Travel Plan (30分) C++实现(求最短路径+最小权重,Dijkstra算法邻接表版,类似甲级1003题)

题目 A traveler’s map gives the distances between cities along the highways, together with the cost of each highway. Now you are supposed to write a program to help a traveler to decide the shortest path between his/her starting city and the destination. …

hihocoder73 编程练习赛 D 好的字符串

1 /*2 Source :hihocoder 编程练习赛73 D 好的字符串3 Problem :问长度为n的数字串中&#xff0c;有多少个包含子串s恰好一次。可以有前导04 Solution :DP. 利用dp[2][i][j]表示当前匹配了前i个字符&#xff0c;且末尾的j个字符和s的前j个字符是匹配的。然后用0,1来表…

thread java api_JAVA进阶系列 - 并发编程 - 第5篇 Thread API

在上一篇中介绍了 Thread 类的构造方法&#xff0c;可是光有构造方法也不够&#xff0c;我们还得再学习多一些该类常用的 API 才行&#xff0c;这样才能对该类有更深刻的了解&#xff0c;同时也能让我们有更多的选择。Thread类提供的API有几十个&#xff0c;由于篇幅问题&#…