一种排序

描述
现在有很多长方形,每一个长方形都有一个编号,这个编号可以重复;还知道这个长方形的宽和长,编号、长、宽都是整数;现在要求按照一下方式排序(默认排序规则都是从小到大);

1.按照编号从小到大排序

2.对于编号相等的长方形,按照长方形的长排序;

3.如果编号和长都相同,按照长方形的宽排序;

4.如果编号、长、宽都相同,就只保留一个长方形用于排序,删除多余的长方形;最后排好序按照指定格式显示所有的长方形;
输入
第一行有一个整数 0<n<10000,表示接下来有n组测试数据;
每一组第一行有一个整数 0<m<1000,表示有m个长方形;
接下来的m行,每一行有三个数 ,第一个数表示长方形的编号,

第二个和第三个数值大的表示长,数值小的表示宽,相等
说明这是一个正方形(数据约定长宽与编号都小于10000);
输出
顺序输出每组数据的所有符合条件的长方形的 编号 长 宽
样例输入
1
8
1 1 1
1 1 1
1 1 2
1 2 1
1 2 2
2 1 1
2 1 2
2 2 1
样例输出
1 1 1
1 2 1
1 2 2
2 1 1
2 2 1

//做这个题一定要看清题目 第二个和第三个数值大的表示长,数值小的表示宽  说明你在输入数据时要自己找下长和宽
#include <stdio.h>
#include <stdlib.h>

typedef struct note 
{
	int number;
	int length;
	int wide;
}Rectangular;

int compare(const void *a, const void *b)
{
	Rectangular tTemp1 = (*(Rectangular *)a);
	Rectangular tTemp2 = (*(Rectangular *)b);

	if(tTemp1.number != tTemp2.number)
		return tTemp1.number - tTemp2.number;
	else if(tTemp1.length != tTemp2.length)
		return tTemp1.length - tTemp2.length;
	else
		return tTemp1.wide - tTemp2.wide;

}

int diff(Rectangular tTemp1, Rectangular tTemp2)
{
	if((tTemp1.number == tTemp2.number) && (tTemp1.length ==tTemp2.length) && (tTemp1.wide == tTemp2.wide) )
		return 1;
	else 
		return 0;
}

int main()
{
	Rectangular tArray[1002];
	int n;
	int m;
	int i;
	int t;
	Rectangular tTemp;

	scanf("%d", &n);

	while(n--){
		
		scanf("%d", &m);
		
		for(i = 0; i < m; i++){
			scanf("%d %d %d",&tArray[i].number, &tArray[i].length, &tArray[i].wide);
			if(tArray[i].length < tArray[i].wide){
				t = tArray[i].length;
				tArray[i].length = tArray[i].wide;
				tArray[i].wide = t;
			}
		}
		qsort(tArray, m, sizeof(Rectangular), compare);


		for(i = 0; i < m; i++){
			if(i == 0)
				printf("%d %d %d\n",tArray[i].number, tArray[i].length, tArray[i].wide);
			else{
				if(diff(tArray[i], tArray[i - 1]))
					continue;
				else
					printf("%d %d %d\n",tArray[i].number, tArray[i].length, tArray[i].wide);
			}
		}

	}
	
	return 0;
}





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值