PAT甲级——A1028 List Sorting
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<string>
#include <vector>
#include<map>
#include <algorithm>
using namespace std;
struct Node
{
int ID, grade;
string name;
};
typedef bool(*funptr)(Node a, Node b);
bool cmp1(Node a, Node b)
{
return a.ID < b.ID;
}
bool cmp2(Node a, Node b)
{
return (a.name == b.name) ? a.ID < b.ID : a.name < b.name;
}
bool cmp3(Node a, Node b)
{
return (a.grade == b.grade) ? a.ID < b.ID : a.grade < b.grade;
} int main()
{
int N, C;
cin >> N >> C;
vector<Node>v;
funptr ptr[] = { &cmp1,&cmp2,&cmp3 };
for (int i = ; i < N; ++i)
{
Node node;
cin >> node.ID >> node.name >> node.grade;
v.push_back(node);
}
//使用函数指针
//sort(v.begin(), v.end(), *(ptr[C - 1])); //使用普通方法
if (C == )
sort(v.begin(), v.end(), [](Node a, Node b) {return a.ID < b.ID; });
else if (C == )
sort(v.begin(), v.end(), [](Node a, Node b) {return (a.name == b.name) ? a.ID < b.ID : a.name < b.name; });
else
sort(v.begin(), v.end(), [](Node a, Node b) {return (a.grade == b.grade) ? a.ID < b.ID : a.grade < b.grade; });
for (auto a : v)
cout << a.ID << " " << a.name << " " << a.grade << endl;
return ;
}
PAT甲级——A1028 List Sorting的更多相关文章
- PAT 甲级 1028 List Sorting (25 分)(排序,简单题)
1028 List Sorting (25 分) Excel can sort records according to any column. Now you are supposed to i ...
- PAT 甲级 1028. List Sorting (25) 【结构体排序】
题目链接 https://www.patest.cn/contests/pat-a-practise/1028 思路 就按照 它的三种方式 设计 comp 函数 然后快排就好了 但是 如果用 c++ ...
- PAT甲级题解(慢慢刷中)
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- 【转载】【PAT】PAT甲级题型分类整理
最短路径 Emergency (25)-PAT甲级真题(Dijkstra算法) Public Bike Management (30)-PAT甲级真题(Dijkstra + DFS) Travel P ...
- PAT甲级题分类汇编——理论
本文为PAT甲级分类汇编系列文章. 理论这一类,是让我觉得特别尴尬的题,纯粹是为了考数据结构而考数据结构.看那Author一栏清一色的某老师,就知道教数据结构的老师的思路就是和别人不一样. 题号 标题 ...
- PAT甲级题分类汇编——序言
今天开个坑,分类整理PAT甲级题目(https://pintia.cn/problem-sets/994805342720868352/problems/type/7)中1051~1100部分.语言是 ...
- PAT甲级:1089 Insert or Merge (25分)
PAT甲级:1089 Insert or Merge (25分) 题干 According to Wikipedia: Insertion sort iterates, consuming one i ...
- PAT甲级1131. Subway Map
PAT甲级1131. Subway Map 题意: 在大城市,地铁系统对访客总是看起来很复杂.给你一些感觉,下图显示了北京地铁的地图.现在你应该帮助人们掌握你的电脑技能!鉴于您的用户的起始位置,您的任 ...
- PAT甲级1127. ZigZagging on a Tree
PAT甲级1127. ZigZagging on a Tree 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二叉树可以通过给定的一对后序和顺序遍历序列来确定.这是一个简单的标准程序,可以按 ...
随机推荐
- memcpy函数实现中的优化
今天浏览Google面试题的时候,有看到一个memcpy的实现,以及如何去优化memcpy. 我对memcpy的实现的记忆就是,拷贝的时候需要从后往前拷贝,为何防止内存重叠. 但是如果去优化它我没有想 ...
- swiper在loop模式,当轮播到最后一张图时候,做其他事件
1.引入文件: <link rel="stylesheet" href="css/swiper.min.css"> <script src=& ...
- POJ-1260-Pearls-dp+理解题意
In Pearlania everybody is fond of pearls. One company, called The Royal Pearl, produces a lot of jew ...
- day 83 Vue学习之五DIY脚手架、webpack使用、vue-cli的使用、element-ui
Vue学习之五DIY脚手架.webpack使用.vue-cli的使用.element-ui 本节目录 一 vue获取原生DOM的方式 二 DIY脚手架 三 vue-cli脚手架的使用 四 we ...
- socket远程执行命令
两个脚本模拟远程执行命令 cmd_server.py import socket import subprocess # 运行系统命令 sk = socket.socket() addess = (' ...
- JS之缓冲动画
原素材 main.html <!DOCTYPE html> <html lang="en"> <head> <link href=&quo ...
- 6_2.springboot2.x整合Druid和配置数据源监控
简介 Druid首先是一个数据库连接池.Druid是目前最好的数据库连接池,在功能.性能.扩展性方面,都超过其他数据库连接池,包括DBCP.C3P0.BoneCP.Proxool.JBoss Data ...
- 使用openSUSE Leap 42.2小记
闪存记录 在2017年04月10日开始想用openSuSE. 2017年04月10日开始找资料制作U盘安装openSUSE. 是在windows 7中用imageWrite.exe 软件制作的.安装的 ...
- 修改cmd命令默认路径
未修改之前: 修改方法: 1.win+r打开运行对话框,输入 regedit 打开注册表编辑器 2.在注册表中找到:HKEY_CURRENT_USER\Software\Microsoft\Comma ...
- 菜鸟安装 CocoaPods
在 iOS 项目开发中,经常会用到第三方的源代码,CocoaPods 就是为了方便管理这些源码的工具. 在官方教程里面,安装看起来非常简单 $ [sudo] gem install cocoapods ...