sort函数使用
万能头文件 #include<bits/stdc++.h>
函数有三个要素
1.要排序的数组的起始地址
2.数组的结束地址
3.第三个参数是排序的方法,可以不写,默认是从小到大
从大到小的写法:sort(a,a+n,greater<要进行排序的数据类型>())
例如一个数组:int a[]={1,2,4,3,6} sort(a,a+5)/这可以理解为一个左闭右开的区间/
结构体
把不同类型的数但是可能是有关联的一组数据当成一个整体处理
声明格式:
struct 类型名{
数据类型 成员1
数据类型 成员2
}变量名;注意分号
使用方法
1.先定义结构体类型,再定义结构体数组;
struct student{
char name[100];
int num;
int age;};
struct student stu1;
2.两者同时定义;
struct student{
char name[100];
int num;
int age;}stu1,stu2;
结构体排序
cmp函数
两个结构体比较
int cmp(const struct stu a,stu b)
{
return a.age>b.age;//从大到小
}
如果相等再按别的方法排序的话用if 条件语句控制
调用方法
sort(stu,stu+n//数组长度,cmp);
林大oj1481
```cpp
#include <bits/stdc++.h>
using namespace std;
struct student
{
int num;
double score;
} a[200];
int cmp(const struct student &c,const struct student &b)
{
return c.score>b.score;
}
int main()
{
int k,m,n;
cin>>n>>k;
for(int i=0; i<n; i++)
cin>>a[i].num>>a[i].score;
sort(a,a+n,cmp);
printf("%d %g",a[k-1].num,a[k-1].score);
//cout << "Hello world!" << endl;
return 0;
}