PAT1028. List Sorting (25)---strcmp
题目链接为:https://www.patest.cn/contests/pat-a-practise/1028
1028. List Sorting (25)
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
题目其实特别简单,应该是考察sort的使用。
<1>由于时间的限制,输入输出最好是scanf 和 printf,否则最后一个测试点将过不去;
<2>同样因为时间的限制,修改vector为数组(不过应该差别不会太大,主要还是<1>)。
论基础知识扎实的重要性,strcmp的结果是返回正数或者负数,而不是返回-1和1。这个点主要用在cmp函数中。
#include <bits/stdc++.h>
using namespace std;
#define maxn 100005
#define For(I,A,B) for(int I = (A); I < (B); I++) class student
{
public:
char id[],name[];
int grade;
};
student stu[maxn];
int n,c;
bool cmp(const student &a,const student &b)
{
if(c == )
{
if(strcmp(a.id,b.id))
return strcmp(a.id,b.id) < ;
}
else if(c == )
{
if(strcmp(a.name,b.name))
return strcmp(a.name,b.name) < ;
return strcmp(a.id,b.id) < ;
}
else if(c == )
{
if(a.grade != b.grade)
return a.grade < b.grade;
return strcmp(a.id,b.id) < ;
}
} int main()
{
freopen("1028.in","r",stdin);
while(scanf("%d%d",&n,&c) != EOF)
{
//stu.clear();
For(i,,n)
{
scanf("%s%s%d",&stu[i].id,&stu[i].name,&stu[i].grade);//>>stu[i].id>>stu[i].name>>stu[i].grade;
}
sort(stu,stu +n,cmp);
For(i,,n)
{
printf("%s %s %d\n",stu[i].id,stu[i].name,stu[i].grade);//cout<<stu[i].id<<" "<<stu[i].name<<" "<<stu[i].grade<<endl;
}
}
return ;
}
PAT1028
PAT1028. List Sorting (25)---strcmp的更多相关文章
- PAT1028. List Sorting (25)
		
id用int,避免了id的strcmp,不然用string就超时. #include <iostream> #include <vector> #include <alg ...
 - PAT 解题报告 1052. Linked List Sorting (25)
		
1052. Linked List Sorting (25) A linked list consists of a series of structures, which are not neces ...
 - PAT1028:List Sorting
		
1028. List Sorting (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Excel ca ...
 - 【PAT】1052 Linked List Sorting (25)(25 分)
		
1052 Linked List Sorting (25)(25 分) A linked list consists of a series of structures, which are not ...
 - Pat 1052 Linked List Sorting (25)
		
1052. Linked List Sorting (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A ...
 - pat1052. Linked List Sorting (25)
		
1052. Linked List Sorting (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A ...
 - PAT 甲级 1052 Linked List Sorting (25 分)(数组模拟链表,没注意到不一定所有节点都在链表里)
		
1052 Linked List Sorting (25 分) A linked list consists of a series of structures, which are not ne ...
 - 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)
		
题目链接:http://pat.zju.edu.cn/contests/pat-a-practise/1028 题目描述: Excel can sort records according to an ...
 
随机推荐
- 常见排序算法-Python实现
			
常见排序算法-Python实现 python 排序 算法 1.二分法 python 32行 right = length- : ] ): test_list = [,,,,,, ...
 - EventBus通信小能手
			
1.EventBus简介 EventBus 是由 greenrobot 组织开发的一个 Android 事件发布/订阅轻量级框架,特点:代码简洁,是一种发布订阅设计模式(观察者设计模式). Even ...
 - 分块编码(Transfer-Encoding: chunked)
			
参考链接: HTTP 协议中的 Transfer-Encoding 分块传输编码 一.背景: 持续连接的问题:对于非持续连接,浏览器可以通过连接是否关闭来界定请求或响应实体的边界:而对于持续连接,这种 ...
 - sql 语句写的行列转换
			
以前面试老遇到一个行列转换的问题,今天没事,顺便记录一下 假设有这样一张表,如下图,创建表就不说了,直接建或者SQL语句都行 sql语句如下 --第一种 select name as 姓名, max( ...
 - JavaScript定时器分析
			
一.事件循环 JavaScript是单线程,同一个时间只能做一件事情,所以执行任务需要排队.如果前一个耗时很长,那么下一个只能等待. 1)两种任务 为了更好的处理任务,JavaScript语言的设计者 ...
 - 关于 this对象 指向问题
			
this 定义:this是包含它的函数作为方法被调用时所属的对象.(1,this所在的函数.2,此函数作为方法被调用.3,this等于调用此函数的对象) this 对象在运行时基于函数的执行环境绑定的 ...
 - 【Scala】Scala之Traits
			
一.前言 前面学习了Scala中包和导入的相关知识点,接着学习Traits(特质) 二.Traits Scala的特质与Java的接口基本相同,当遇到可以使用Java接口的情形,就可以考虑使用特质,S ...
 - appium执行iOS测试脚本并发问题
			
appium1.4.X+iOS9.X+xcode7.X: appium1.4.x+iOS9.x+xcode7.x,这一整套的配置做移动端自动化测试是测试人员常用的测试框架.关于,这一套测试框架的并发问 ...
 - 数据库问题(程序连接mysql错误)
			
今天服务器遇到了一个很熟悉的问题 输入 #mysql -u root -p ERROR 2002 (HY000):Can't connect to local MySQL server 随即上网找寻答 ...
 - NuGet 自定义配置
			
默认配置: 默认配置文件的路径%APPDATA%\NuGet\NuGet.Config (DOS) 或 $ENV:APPDATA\NuGet\NuGet.Config (PowerShell),(例如 ...