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的更多相关文章

  1. PAT 甲级 1028 List Sorting (25 分)(排序,简单题)

    1028 List Sorting (25 分)   Excel can sort records according to any column. Now you are supposed to i ...

  2. PAT 甲级 1028. List Sorting (25) 【结构体排序】

    题目链接 https://www.patest.cn/contests/pat-a-practise/1028 思路 就按照 它的三种方式 设计 comp 函数 然后快排就好了 但是 如果用 c++ ...

  3. PAT甲级题解(慢慢刷中)

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  4. 【转载】【PAT】PAT甲级题型分类整理

    最短路径 Emergency (25)-PAT甲级真题(Dijkstra算法) Public Bike Management (30)-PAT甲级真题(Dijkstra + DFS) Travel P ...

  5. PAT甲级题分类汇编——理论

    本文为PAT甲级分类汇编系列文章. 理论这一类,是让我觉得特别尴尬的题,纯粹是为了考数据结构而考数据结构.看那Author一栏清一色的某老师,就知道教数据结构的老师的思路就是和别人不一样. 题号 标题 ...

  6. PAT甲级题分类汇编——序言

    今天开个坑,分类整理PAT甲级题目(https://pintia.cn/problem-sets/994805342720868352/problems/type/7)中1051~1100部分.语言是 ...

  7. PAT甲级:1089 Insert or Merge (25分)

    PAT甲级:1089 Insert or Merge (25分) 题干 According to Wikipedia: Insertion sort iterates, consuming one i ...

  8. PAT甲级1131. Subway Map

    PAT甲级1131. Subway Map 题意: 在大城市,地铁系统对访客总是看起来很复杂.给你一些感觉,下图显示了北京地铁的地图.现在你应该帮助人们掌握你的电脑技能!鉴于您的用户的起始位置,您的任 ...

  9. PAT甲级1127. ZigZagging on a Tree

    PAT甲级1127. ZigZagging on a Tree 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二叉树可以通过给定的一对后序和顺序遍历序列来确定.这是一个简单的标准程序,可以按 ...

随机推荐

  1. 在类中,调用这个类时,用$this->video_model是不是比每次调用这个类时D('Video')效率更高呢

    在类中,调用这个类时,用$this->video_model是不是比每次调用这个类时D('Video')效率更高呢  

  2. 可拖拽排序的vue组件

    最近在优化一个vue的博客系统,想实现文章列表处的文章拖拽功能.就试了一下awe-dnd vue插件,觉得还挺好用的. 安装 npm install awe-dnd --save 使用 在main.j ...

  3. laravel装饰者模式例子

    interface Decorator{ public function display(); } class XiaoFang implements Decorator { private $nam ...

  4. <Django> MVT三大块之Models(模型)

    1.ORM(对象-关系-映射)---面向对象,不需要面向SQL语句 根据对象的类型生成表结构 将对象.列表的操作,转化成SQL语句 将SQL语句查询的结果转化成对象.列表 目的:实现数据模型与数据库的 ...

  5. Activiti流程图查看

    1.测试用例查看图片 public void viewImage() throws Exception { // 创建仓库服务对对象 RepositoryService repositoryServi ...

  6. LeetCode 28.实现strStr()(Python3)

    题目: 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始).如果不存 ...

  7. Oracle如何用单字段或多字段进行查重

    最近在整理数据形成信用报告,发现重复的数据真的多,梳理都好久.我就做个笔记把去掉重复数据的方法整理下来.方便我后期查阅. 我将我目前已知的两种去重方法分为:视图去重和表去重.原理就是有无rowid这个 ...

  8. 07.07NOIP模拟赛

    考中 考试时不知道自己在想啥.. 拿到第一题:woc组合数学,第二题:woc组合数学,第三题,woc组合数学. 然后开始认真读题…… 我tm真是闲的... 第一题是15年山东省选题,感觉暴力搜索都没法 ...

  9. QQ交流群

  10. ERROR in xxx.js from UglifyJs

    在打包项目的时候,出现如下的错误: 经过搜索找到原因: 这是因为webpack在打包vue文件时没有成功转换ES6的语法. 解决方案 解决方法很简单,加入babel-preset-es2015插件即可 ...