acm练习:矩形排序

来源:互联网 发布:淘宝申请售后服务培训 编辑:程序博客网 时间:2024/05/21 17:09

时间限制:3000 ms | 内存限制:65535 KB
难度:3

描述

现在有很多长方形,每一个长方形都有一个编号,这个编号可以重复;还知道这个长方形的宽和长,编号、长、宽都是整数;现在要求按照一下>方式排序(默认排序规则都是从小到大);
1.按照编号从小到大排序
2.对于编号相等的长方形,按照长方形的长排序;
3.如果编号和长都相同,按照长方形的宽排序;
4.如果编号、长、宽都相同,就只保留一个长方形用于排序,删除多余的长方形;最后排好序按照指定格式显示所有的长方形;

输入

第一行有一个整数 0

输出

顺序输出每组数据的所有符合条件的长方形的 编号 长 宽

样例输入

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>struct rectangle {    int id;    int length;    int width;    int repetition;    int reverse;};struct rectangle a[1000];void exch(struct rectangle *a,int i) {struct rectangle ra=a[i];a[i]=a[i-1];a[i-1]=ra;}void funk1();int comp(int,struct rectangle *); int  main() {    int n;    scanf("%ld",&n);    while(n--) funk1();    return 0;}int comp(int i,struct rectangle *a) {return (a[i].id<a[i-1].id)?1:((a[i].id>a[i-1].id)?0:((a[i].length>a[i-1].length)?0:((a[i].length<a[i-1].length)?1:((a[i].width>a[i-1].width)?0:((a[i].width<a[i-1].width)?1:2)))));}void funk1() {        int m,i,j,k;        scanf("%ld",&m);        for(i=0;i<m;i++) {            a[i].repetition=0;            a[i].reverse=0;            scanf("%ld %ld %ld",&a[i].id,&a[i].length,&a[i].width);            if(a[i].width>a[i].length) {                k=a[i].length;                a[i].length=a[i].width;                a[i].width=k;                a[i].reverse=1;            }        }        for(i=m-1;i>0;i--) {            k=comp(i,a);            switch(k) {            case 1:exch(a,i);break;            case 2:a[i].repetition=1;                    break;            }        }        for(i=2;i<m;i++) {            for(j=i;k=comp(j,a);j--) {                switch(k) {                case 1:exch(a,j);break;                case 2:a[j].repetition=1;                           break;                }            }        }        for(i=0;i<m;i++) {            if(a[i].repetition==1) continue;            printf("%ld %ld %ld\n",a[i].id,a[i].length,a[i].width);        }    }
原创粉丝点击