zoukankan      html  css  js  c++  java
  • C++实现多级排序

    stl中sort详细说明

    实现功能:期末开始4位同学的成绩,按多级排序,排序规则为:按数学从小到大,如果数学相等,则按语文从大到小排列,如果语文相等,则按英语从小到大排列,如果英语相等,则按历史从大到小排烈

      1 #include "stdafx.h"
      2 
      3 #include
      4 
      5 #include
      6 
      7 #include
      8 
      9 typedef struct stu
     10 
     11 {
     12 
     13     char name[24];
     14 
     15     int math;//数学
     16 
     17     int chinese;//语文
     18 
     19     int english;//英语
     20 
     21     int history;//历史
     22 
     23 }Student;
     24 
     25 //按照你想排序的方法排序
     26 
     27 bool cmp(stu l, stu r)
     28 
     29 {
     30 
     31     if (l.math < r.math)//数学从小到大
     32 
     33     {
     34 
     35         return true;
     36 
     37     }
     38 
     39     else if (l.math == r.math)//如果数学相等
     40 
     41     {
     42 
     43         if (l.chinese > r.chinese)//按语文从大到小
     44 
     45         {
     46 
     47             return true;
     48 
     49         }
     50 
     51         else if (l.chinese == r.chinese)//如果语文相等
     52 
     53         {
     54 
     55             if (l.english < r.english)//按英语从小到大
     56 
     57             {
     58 
     59                 return true;
     60 
     61             }
     62 
     63             else if (l.english == r.chinese)//如果英语相等
     64 
     65             {
     66 
     67                 if (l.history > r.history)//按历史从大到小
     68 
     69                 {
     70 
     71                     return true;
     72 
     73                 }
     74 
     75                 else if (l.history == r.history)
     76 
     77                 {
     78 
     79                     return false;
     80 
     81                 }
     82 
     83             }
     84 
     85         }
     86 
     87     }
     88 
     89     return false;
     90 
     91 }
     92 
     93 using namespace std;
     94 
     95 //  小明 73, 80, 86, 79
     96 
     97 //小花 77, 78, 60, 90
     98 
     99 //  小刚 73, 79, 90, 90
    100 
    101 //  小白 77, 78, 40, 89
    102 
    103 //排序后
    104 
    105 //  小明 73, 80, 86, 79
    106 
    107 //  小刚 73, 79, 90, 90
    108 
    109 //  小白 77, 78, 40, 89
    110 
    111 //小花 77, 78, 60, 90
    112 
    113 Student students[4] = { { "小明", 73, 80, 86, 79 }
    114 
    115 , { "小花", 77, 78, 60, 90 }
    116 
    117 , { "小刚", 73, 79, 90, 90 }
    118 
    119 , { "小白", 77, 78, 40, 89 } };
    120 
    121 int _tmain(int argc, _TCHAR* argv[])
    122 
    123 {
    124 
    125     cout << "排序前:===========================================" << endl;
    126 
    127     for (Student s : students)
    128 
    129     {
    130 
    131         cout << s.name << ""
    132 
    133             << s.math << ""
    134 
    135             << s.chinese << ""
    136 
    137             << s.english << ""
    138 
    139             << s.history << endl;
    140 
    141     }
    142 
    143     sort(students, students + 4, cmp);
    144 
    145     cout << "排序后:===========================================" << endl;
    146 
    147     for (Student s : students)
    148 
    149     {
    150 
    151         cout << s.name << ""
    152 
    153             << s.math << ""
    154 
    155             << s.chinese << ""
    156 
    157             << s.english << ""
    158 
    159             << s.history << endl;
    160 
    161     }
    162 
    163     getchar();
    164 
    165     return 0;
    166 
    167 }
    View Code

    效果如:

    如果您觉得文章不错,不妨给个打赏,写作不易,感谢各位的支持。您的支持是我最大的动力,谢谢!!! 

     

      


    很重要--转载声明

    1. 本站文章无特别说明,皆为原创,版权所有,转载时请用链接的方式,给出原文出处。同时写上原作者:朝十晚八 or Twowords
    2. 如要转载,请原文转载,如在转载时修改本文,请事先告知,谢绝在转载时通过修改本文达到有利于转载者的目的。 

    多级排序
  • 相关阅读:
    One SQL to Rule Them All – an Efficient and Syntactically Idiomatic Approach to Management of Streams and Tables(中英双语)
    Spark 公共篇-InterfaceStability
    ANTLR v4 专业术语集
    Apache Spark as a Compiler: Joining a Billion Rows per Second on a Laptop(中英双语)
    Deep Dive into Spark SQL’s Catalyst Optimizer(中英双语)
    What’s new for Spark SQL in Apache Spark 1.3(中英双语)
    Scala 隐式(implicit)详解
    Introducing Apache Spark Datasets(中英双语)
    darknet-yolov3训练自己的数据集(转)
    NVIDIA显卡,显卡驱动版本,CUDA版本,cudnn版本之间兼容关系及如何选择
  • 原文地址:https://www.cnblogs.com/swarmbees/p/5621590.html
Copyright © 2011-2022 走看看