一种排序
时间限制:
3000 ms | 内存限制:
65535 KB
难度:
3
-
描述
-
现在有很多长方形,每一个长方形都有一个编号,这个编号可以重复;还知道这个长方形的宽和长,编号、长、宽都是整数;现在要求按照一下方式排序(默认排序规则都是从小到大);
1.按照编号从小到大排序
2.对于编号相等的长方形,按照长方形的长排序;
3.如果编号和长都相同,按照长方形的宽排序;
4.如果编号、长、宽都相同,就只保留一个长方形用于排序,删除多余的长方形;最后排好序按照指定格式显示所有的长方形;-
输入
-
第一行有一个整数 0<n<10000,表示接下来有n组测试数据;
每一组第一行有一个整数 0<m<1000,表示有m个长方形;
接下来的m行,每一行有三个数 ,第一个数表示长方形的编号,
第二个和第三个数值大的表示长,数值小的表示宽,相等
说明这是一个正方形(数据约定长宽与编号都小于10000);
输出
-
顺序输出每组数据的所有符合条件的长方形的 编号 长 宽
#include <stdio.h> #include <stdlib.h> struct Rectangle { int Num; int len_long; int len_wide; }R[1001]; int cmp(const void *a, const void *b) { struct Rectangle * p = (Rectangle *)a; struct Rectangle * q = (Rectangle *)b; if(p->Num != q->Num) return p->Num - q->Num; else if(p->len_long != q->len_long) return p->len_long - q->len_long; else return p->len_wide - q->len_wide; } int main() { int t, n, num, l, w; scanf("%d", &t); while(t--) { scanf("%d", &n); int i; for(i = 0; i < n; i++) { scanf("%d %d %d", &num, &l, &w); if(l < w) { int t; t = l; l = w; w = t; } R[i].Num = num; R[i].len_long = l; R[i].len_wide = w; } qsort(R, n, sizeof(R[0]), cmp); Rectangle t = R[0]; printf("%d %d %d\n", t.Num, t.len_long, t.len_wide); for(i = 1; i < n; i++) { if(R[i].Num != t.Num || R[i].len_long != t.len_long || R[i].len_wide != t.len_wide) { t = R[i]; printf("%d %d %d\n", t.Num, t.len_long, t.len_wide); } } } return 0; }
-
第一行有一个整数 0<n<10000,表示接下来有n组测试数据;