• nyoj_8_一种排序_201311251238


    一种排序

    时间限制:3000 ms  |           内存限制:65535 KB
    难度:3
     
    描述
    现在有很多长方形,每一个长方形都有一个编号,这个编号可以重复;还知道这个长方形的宽和长,编号、长、宽都是整数;现在要求按照一下方式排序(默认排序规则都是从小到大);
    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
    来源
    经典题目
    上传者
    iphxer
     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #include <string.h>
     4 
     5 typedef struct IN
     6 {
     7     int num;
     8     int x;
     9     int y;
    10 }IN;
    11 IN s[1010];
    12 
    13 int cmp(const void *a,const void *b)
    14 {
    15     struct IN *c = (IN *)a;
    16     struct IN *d = (IN *)b;
    17     if(c->num!=d->num)
    18     return c->num - d->num;
    19     else
    20     {
    21         if(c->x!=d->x)
    22         return c->x - d->x;
    23         else
    24         return c->y - d->y;
    25     }
    26 }
    27 
    28 int main()
    29 {
    30     int T;
    31     scanf("%d",&T);
    32     while(T--)
    33     {
    34         int i,j,k,n;
    35         int a,b,c;
    36         memset(s,0,sizeof(s));
    37         scanf("%d",&n);
    38         for(i=0;i<n;i++)
    39         {
    40             scanf("%d %d %d",&s[i].num,&b,&c);
    41             if(c>b)
    42             {s[i].x=c;s[i].y=b;}
    43             else
    44             {s[i].x=b;s[i].y=c;}
    45         }
    46         qsort(s,n,sizeof(s[0]),cmp);
    47         printf("%d %d %d
    ",s[0].num,s[0].x,s[0].y);
    48         for(i=1;i<n;i++)
    49         {
    50             if(s[i].num==s[i-1].num&&s[i].x==s[i-1].x&&s[i].y==s[i-1].y)
    51             continue;
    52             else
    53             printf("%d %d %d
    ",s[i].num,s[i].x,s[i].y);
    54         }        
    55     }
    56     return 0;
    57 }

    qsort对三级结构体的排序

  • 相关阅读:
    Leetcode: Construct Binary Tree from Preorder and Inorder Traversal
    Leetcode: Flatten Binary Tree to Linked List
    Leetcode: Binary Tree Level Order Transversal II
    Leetcode: Binary Tree Level Order Traversal
    Leetcode: Binary Tree Postorder Transversal
    Leetcode: Binary Tree Inorder Transversal
    Leetcode: Word Break II
    Leetcode: Word Break
    Leetcode: Maximum Subarray
    WDS 三种模式
  • 原文地址:https://www.cnblogs.com/xl1027515989/p/3441403.html
Copyright © 2020-2023  润新知