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. F - GCD - Extreme (II) UVA - 11426

    Given the value of N, you will have to find the value of G. The definition of G is given below:

  2. 三种方法实现MNIST 手写数字识别

    MNIST数据集下载: import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data mnist ...

  3. Android按钮绑定四种方式

    public class MainActivity extends Activity implements OnClickListener{ @Override protected void onCr ...

  4. Java开发系列-电子邮箱

    概述 电子邮箱就是在邮箱服务器上开启的一块空间.邮箱服务器就是一个安装在计算机的服务软件,提供有邮件服务. 邮箱的发送流程 现在tom要发送一份邮件给jerry,首先tom将邮件通过客户端连接自己设置 ...

  5. COGITATE | 分析当前热门软件的创新

    热门软件分析实例一——Github [简介] gitHub是一个面向开源及私有软件项目的托管平台,因为只支持git 作为唯一的版本库格式进行托管,故名gitHub.作为一个分布式的版本控制系统,在Gi ...

  6. T2980 LR棋盘【Dp+空间/时间优化】

    Online Judge:未知 Label:Dp+滚动+前缀和优化 题目描述 有一个长度为1*n的棋盘,有一些棋子在上面,标记为L和R. 每次操作可以把标记为L的棋子,向左移动一格,把标记为R的棋子, ...

  7. [转]使用TortoiseGit处理代码冲突

    场景一  user0 有新提交 user1 没有pull -> 写新代码 -> pull -> 提示有冲突   解决办法一 -> stash save(把自己的代码隐藏存起来) ...

  8. Linux命令查看文件内容

    cat:一次性顺序显示文件所有内容和 cat filename tac:一次性倒序显示文件所有内容和 tac filename head:显示文件开头的若干行内容 head -n filename t ...

  9. mybatis 一对多和一对一写法注意事项

    <resultMap id="ChartResultMap" type="com.qif.dsa.ucenter.planinfo.entity.ChartDate ...

  10. centos 6 编译glibc-2.14

    1.查看系统版本, 升级系统基本lib库 [root@test ~]# cat /etc/redhat-release CentOS release 6.5 (Final) 2.查看系统glibc支持 ...