基础实验7-2.3 德才论 (25 分)

本文介绍了如何使用C++编程实现结构体`p`的排序,通过比较f1、f2、f3和c字段,同时考虑字符串`s`,实现了一种复合排序算法。展示了如何在`main`函数中读取输入并调用自定义的`cmp`函数进行排序。
摘要由CSDN通过智能技术生成

基础实验7-2.3 德才论 (25 分)

结构体排序

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string> 
using namespace std;
struct p{
	int a,b,c,f1,f2,f3;
	string s;
}t[101000];
bool cmp(p x,p y){
	if(x.f1>y.f1)
		return 1;
	else if(x.f1==y.f1){
		if(x.f2>y.f2)
			return 1;
		else if(x.f2==y.f2){
			if(x.f3>y.f3)
				return 1;
			else if(x.f3==y.f3){
				if(x.c>y.c)
					return 1;
				else if(x.c==y.c)
				{
					if(x.a>y.a)
						return 1;
					else if(x.a==y.a)
						return x.s<y.s;
				}
			}
		}
	}
	return 0;
}
int main()
{    
	int n,l,h,ret=0;
	cin>>n>>l>>h;
	while(n--)
	{
		string s1;
		int x,y;
		cin>>s1>>x>>y;
		if(x>=l&&y>=l){
			t[ret].s=s1;
			t[ret].a=x;
			t[ret].b=y;
			t[ret].c=x+y;
			if(x>=h&&y>=h)
				t[ret].f1=1;
			else if(x>=h)
				t[ret].f2=1;
			else if(x>=y)
				t[ret].f3=1;	
			ret++;
		}
	}
	sort(t,t+ret,cmp);
	cout<<ret<<endl;
	for(int i=0;i<ret;++i)
		cout<<t[i].s<<" "<<t[i].a<<" "<<t[i].b<<endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值