南阳OJ-一种排序

版权声明:都是自己写的喔~ https://blog.csdn.net/qq_42811706/article/details/87906609
#include<iostream>
#include<algorithm>
using namespace std;
struct rectangle
{
	int num;
	int llong;
	int sshort;
};
bool company(rectangle a,rectangle b)
{
	if(a.num!=b.num)
		return a.num<b.num;
	else if(a.llong!=b.llong)
		return a.llong<b.llong;
	else if(a.sshort!=b.sshort)
		return a.sshort<b.sshort;
}
int main ()
{
	int n;
	cin>>n;
	while(n--)
	{
		int m;
		cin>>m;
		rectangle r[1005];
		for(int i=0;i<m;i++)
		{
			int a,b,c;
			cin>>a>>b>>c;
			r[i].num =a;
			if(b>c)
			{
				r[i].llong =b;
				r[i].sshort =c;
			} 
			else
			{
				r[i].sshort =b;
				r[i].llong =c;
			}
		}
		sort(r,r+m,company);
		for(int i=0;i<m;i++){
			if(r[i].num==r[i-1].num &&r[i].llong ==r[i-1].llong && r[i].sshort==r[i-1].sshort ) continue;
			cout<<r[i].num <<" "<<r[i].llong<<" "<<r[i].sshort<<endl;
		}
	}
	return 0;
}

和德才论的做法差不多,都是一个元素有好多属性,根据每一个属性进行排序。

这种题可以考虑建立结构体,对结构体用sort函数进行排序,排序的方法自己写一个函数。
自定义排序函数中,如果按递增排列则返回小于号(小的在前),如果按递减排列则返回大于号(大的在前)。

猜你喜欢

转载自blog.csdn.net/qq_42811706/article/details/87906609