结构体排序

结构体数组

#include<bits/stdc++.h>
using namespace std;
const int N=1010;

struct Student1{
string name;
int age,int score;
};
Student q1[N];
struct Student2{
string name;
int age,int score;
}q2[N];
struct {
string name;
int age,int score;
}q[N];
#include<bits/stdc++.h>
using namespace std;
const int N=1010;
int n;
struct Student{
    string name;
    int age, score;
    bool operator< (const Student& t)const{
        if(score!=t.score)return score<t.score;
        if(name!=t.name)return name<t.name;
        return age<t.age;
    }
};

int main(){
    cin>>n;
    Student q[N];
    for(int i=0;i<n;i++){
        cin>>q[i].name>>q[i].age>>q[i].score;
    }
    sort(q,q+n);
    for(int i=0;i<n;i++)
    cout<<q[i].name<<" "<<q[i].age<<" "<<q[i].score<<endl;
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
const int N=1010;
int n;
struct Student{
    string name;
    int age, score;
    
}q[N];
bool cmpone(Student a,Student b){
    if(a.score!=b.score)return a.score<b.score;//从小到大排序
    if(a.name!=b.name)return a.name<b.name;
    return a.age<b.age;
}
int main(){
    cin>>n;
    Student q[N];
    for(int i=0;i<n;i++){
        cin>>q[i].name>>q[i].age>>q[i].score;
    }
    sort(q,q+n,cmpone);
    for(int i=0;i<n;i++)
    cout<<q[i].name<<" "<<q[i].age<<" "<<q[i].score<<endl;
    return 0;
}

在这里插入代码片在这里插入图片描述

#include<iostream>
//#include<bits/stdc++.h>
#include<algorithm>
#include<vector>
using namespace std;
struct  stu {
	int ch, math,eng, sum;
	int id;
};
bool cmp(stu a,stu b)
{
	if (a.sum > b.sum)
		return 1;
	else if (a.sum < b.sum)
		return 0;
	else {
		if (a.ch > b.ch)
			return 1;
		else if (a.ch < b.ch)
			return 0;
		else{
			if (a.id <= b.id)
				return 1;
			else
				return 0;

		}
	}
}
int main()
{
	int n;
	cin >> n;
	//vector<stu> s;
	stu s[300];
	int a, b, c;
	for (int i = 1; i <=n; i++) {
			s[i].id = i;
			cin >>s[i].ch >> s[i].math >> s[i].eng;
			s[i].sum = s[i].ch + s[i].math + s[i].eng;
	}
	sort(s+1, s+n+1, cmp);
	for (int i = 1; i <= 5; i++)
		cout << s[i].id << " " << s[i].sum << endl;
	system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值