zoukankan      html  css  js  c++  java
  • 对结构体二级排序的应用 —— 一种排序问题

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

    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>
    #define M 1000
    struct info
    {
        int list;
        int lenth;
        int wide;
    };
    struct info shape[M];    
    int cmp(const void *x, const void *y)
    {
        
        //    struct info *a = (info *)x;易错点:info前没加struct。记住 结构体类型名 =  关键字struct + 结构体名 
        //    struct info *b = (info *)y;
            struct info *a = (struct info *)x;
            struct info *b = (struct info *)y;
            if(a -> list != b -> list)
            {
                    return a -> list  -  b -> list;
            }
            if(a -> lenth != b -> lenth)
            {
                    return a -> lenth -  b -> lenth;
            }
            return a -> wide  -  b -> wide;
            
    }
    
    int main()
    {
        
            int x, n, i, t;
            scanf("%d", &x);
            while(x--){
                
                    scanf("%d", &n);
                    for(i = 0; i < n; i++){
                        
                            scanf("%d%d%d", &shape[i].list, &shape[i].lenth, &shape[i].wide);
                            if(shape[i].lenth < shape[i].wide)
                            {
    
                                    t = shape[i].lenth;
                                    shape[i].lenth = shape[i].wide;
                                    shape[i].wide = t;
                                
                            }
                            
                    }
                    qsort(shape, n, sizeof(shape[0]), cmp);
                    for(i = 0; i < n; i++){
                        
                            if(i != 0 && shape[i].list == shape[i - 1].list && shape[i].lenth == shape[i - 1].lenth && shape[i].wide == shape[i - 1].wide)
                                continue;
                            printf("%d %d %d
    ", shape[i].list, shape[i].lenth,shape[i].wide);
                            
                    }
                    //putchar('
    ');
                    
            }
            return 0;
    } 
    View Code
  • 相关阅读:
    896. Monotonic Array单调数组
    865. Smallest Subtree with all the Deepest Nodes 有最深节点的最小子树
    489. Robot Room Cleaner扫地机器人
    JavaFX
    《Python CookBook2》 第一章 文本
    《Python CookBook2》 第一章 文本
    《Python CookBook2》 第一章 文本
    《Python CookBook2》 第一章 文本
    《Python CookBook2》 第一章 文本
    《Python CookBook2》 第一章 文本
  • 原文地址:https://www.cnblogs.com/the-one/p/3239913.html
Copyright © 2011-2022 走看看