PAT (Advanced Level) Practice 1028 List Sorting (25 分) (自定义排序)
Excel can sort records according to any column. Now you are supposed to imitate this function.
Input Specification:
Each input file contains one test case. For each case, the first line contains two integers N (≤) 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 Specification:
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 <algorithm>
#include <string>
#include <cstring>
using namespace std;
struct node
{
string str1,str2;
int str3;
}a[];
int cmp1(node x,node y)
{
return x.str1<y.str1;
}
int cmp2(node x,node y)
{
if(x.str2==y.str2) return x.str1<y.str1;
return x.str2<y.str2;
}
int cmp3(node x,node y)
{
if(x.str3==y.str3) return x.str1<y.str1;
return x.str3<y.str3;
}
int main()
{
int n,m;
while(cin>>n>>m){
for(int i=;i<n;i++){
cin>>a[i].str1>>a[i].str2>>a[i].str3;
}
if(m==){
sort(a,a+n,cmp1);
}else if(m==){
sort(a,a+n,cmp2);
}else if(m==){
sort(a,a+n,cmp3);
}
for(int i=;i<n;i++){
cout<<a[i].str1<<" "<<a[i].str2<<" "<<a[i].str3<<endl;
}
}
return ;
}
PAT (Advanced Level) Practice 1028 List Sorting (25 分) (自定义排序)的更多相关文章
- PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分) 凌宸1642
PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分) 凌宸1642 题目描述: Shuffling is a procedure us ...
- PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642
PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642 题目描述: The task is really simple: ...
- PAT (Advanced Level) Practice 1041 Be Unique (20 分) 凌宸1642
PAT (Advanced Level) Practice 1041 Be Unique (20 分) 凌宸1642 题目描述: Being unique is so important to peo ...
- PAT (Advanced Level) Practice 1015 Reversible Primes (20 分) 凌宸1642
PAT (Advanced Level) Practice 1015 Reversible Primes (20 分) 凌宸1642 题目描述: A reversible prime in any n ...
- PAT 甲级 1028 List Sorting (25 分)(排序,简单题)
1028 List Sorting (25 分) Excel can sort records according to any column. Now you are supposed to i ...
- PAT (Advanced Level) 1052. Linked List Sorting (25)
简单题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> ...
- PAT (Advanced Level) Practice 1003 Emergency 分数 25 迪杰斯特拉算法(dijkstra)
As an emergency rescue team leader of a city, you are given a special map of your country. The map s ...
- PAT (Advanced Level) Practice 1120 Friend Numbers (20 分) (set)
Two integers are called "friend numbers" if they share the same sum of their digits, and t ...
- PAT (Advanced Level) Practice 1152 Google Recruitment (20 分)
In July 2004, Google posted on a giant billboard along Highway 101 in Silicon Valley (shown in the p ...
随机推荐
- Linux文本三剑客
grep 文本过滤工具. 作用: 文本搜索工具,根据用户指定的行进行匹配检查,打印匹配到的行. 模式: 由正则表达式字符及文本字符所编写的过滤条件. grep的使用 语法: grep [OPTION ...
- 解决Python2.7的UnicodeEncodeError: 'ascii' codec can't encode异常错误
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128) ...
- Linux恢复删除的文件
linux恢复删除的文件 先介绍下一些文件的基本概念: · 文件实际上是一个指向inode的链接, inode链接包含了文件的所有属性, 比如权限和所有者, 数据块地址(文件存储在磁盘 ...
- springboot 基于Tomcate的自启动流程
Springboot 内置了Tomcat的容器,我们今天来说一下Springboot的自启动流程. 一.Spring通过注解导入Bean大体可分为四种方式,我们主要来说以下Import的两种实现方法: ...
- Web渗透测试漏洞手册及修复建议
Web渗透测试漏洞手册及修复建议 0x0 配置管理 0x01 HTTP方法测试 漏洞介绍: 目标服务器启用了不安全的传输方法,如PUT.DELETE等,这些方法表示可能在服务器上使用了 WebDAV, ...
- MacBook通过SSH远程访问Parallel中的Ubuntu简明教程
作为一个前端,后端也需要了解,最终选择PHP入手学习,本来想选择Python,思前想后还是PHP作为Web开发比较合适,环境最终选择Ubuntu开发,由于是第一次,遇到不少坑,经过不懈的努力不断Goo ...
- JS循环解决任意日期间的间隔天数
用JS循环解决任意日期间的间隔天数,并求截止日期是周几 y1=1900 m1=1 d1=1 y2=2000 m2=5 d2=3 days=0 ydays=0 mdays=0 ddays=d2-d1 f ...
- hadoop3自学入门笔记(3)-java 操作hdfs
1.core-site.xml <configuration> <property> <name>fs.defaultFS</name> <val ...
- Linux内核的LED设备驱动框架【转】
/************************************************************************************ *本文为个人学习记录,如有错 ...
- MFC/QT 学习笔记(二)——MFC入门
MFC以C++形式封装了Windows API //实践 编写MFC需要的头文件#include <afxwin.h> 程序执行流程: 实例化应用程序对象(有且只有一个) 执行程序入口函数 ...