多重排序

学号和成绩,按成绩从小到大排序,成绩相同按学号从小到大排序

用 qsort(数组名,数组元素个数,sizeof(数组类型),cmp)

其中cmp是需要自己写的函数

#include<stdio.h>
#include<stdlib.h> 
typedef struct  
{  
    int y;  
    int x;  
}NODE;  
NODE node[101];
int cmp(const void *a,const void *b)        
{     
       if( ((NODE*)a)->x != ((NODE*)b)->x)  
          return  ((NODE*)a)->x > ((NODE*)b)->x ? 1 : -1;  
       else  
        return  ((NODE*)a)->y > ((NODE*)b)->y ? 1 : -1;        
}   

int main()
{
   int n,i,j;
   while(scanf("%d",&n)!=EOF)
   {
     for(i=0;i<n;i++)
       scanf("%d%d",&node[i].y,&node[i].x);
     qsort(node,n,sizeof(NODE),cmp); 
     for(i=0;i<n;i++)
      printf("%d %d\n",node[i].y,node[i].x);
   }
   return 0;  
}
        


### 集合多重排序的概念 集合多重排序通常指的是在一个包含重复元素的数据结构中,按照特定条件对这些元素进行多次不同的排序操作。这种需求可能涉及多个维度的比较或者基于不同属性的优先级排列。 --- #### 使用 `unordered_multiset` 进行多重排序 虽然 `unordered_multiset` 是一种无序容器[^1],但它可以用来存储具有相同键值的多个元素。如果需要对其进行排序,则可以通过将其转换为其他有序容器(如 `vector` 或 `multiset`),再应用自定义排序逻辑来完成。 以下是具体实现: ```cpp #include <iostream> #include <unordered_set> #include <vector> #include <algorithm> int main() { std::unordered_multiset<int> ums = {4, 2, 5, 2, 3, 4}; // 创建 unordered_multiset // 将 unordered_multiset 转换为 vector std::vector<int> vec(ums.begin(), ums.end()); // 自定义排序规则:升序排列 std::sort(vec.begin(), vec.end()); // 输出排序后的结果 for (const auto& elem : vec) { std::cout << elem << " "; } std::cout << "\n"; // 可以进一步修改排序规则,例如降序排列 std::sort(vec.rbegin(), vec.rend()); // 再次输出结果 for (const auto& elem : vec) { std::cout << elem << " "; } std::cout << "\n"; return 0; } ``` 上述代码展示了如何通过将 `unordered_multiset` 的内容复制到 `std::vector` 中并调用标准库函数 `std::sort()` 来实现多种排序方式。 --- #### 基于分治思想的多重排序 对于更复杂的场景,比如多维数据或多层嵌套关系下的排序问题,可以考虑使用分治策略。这种方法的核心在于递归地分解问题,并逐步解决子问题后再合并结果[^2]。 假设我们有一个二维数组表示学生的信息(姓名和成绩),希望先按成绩降序排列,当成绩相同时则按名字字典顺序升序排列: ```cpp #include <iostream> #include <vector> #include <string> #include <algorithm> struct Student { std::string name; int score; bool operator<(const Student &other) const { if (score != other.score) return score > other.score; // 成绩降序 return name < other.name; // 名字升序 } }; void divideAndConquerSort(std::vector<Student>& students, int left, int right) { if (left >= right) return; int mid = left + (right - left) / 2; divideAndConquerSort(students, left, mid); divideAndConquerSort(students, mid + 1, right); std::inplace_merge(students.begin() + left, students.begin() + mid + 1, students.begin() + right + 1); } int main() { std::vector<Student> students = {{"Alice", 85}, {"Bob", 92}, {"Charlie", 85}, {"David", 70}}; divideAndConquerSort(students, 0, static_cast<int>(students.size()) - 1); for (const auto& student : students) { std::cout << student.name << ": " << student.score << "\n"; } return 0; } ``` 此程序利用了分治递归的思想,在每次分割之后重新组合已排序的部分。 --- #### 动态规划与多重背包中的排序优化 在某些高级算法领域,例如动态规划求解多重背包问题时,合理的预处理步骤也涉及到排序技术的应用。通过对物品价值密度或其他特性先行排序,能够显著提升后续计算效率[^3]。 以下是一个简化版的例子展示如何根据重量性价比对项目列表预先排序以便更好地应用于 DP 表格构建过程之中: ```cpp #include <bits/stdc++.h> using namespace std; // 定义物品类 class Item { public: double weight; double value; double ratio; Item(double w, double v):weight(w),value(v){ this->ratio=v/w; } }; bool compare(Item a,Item b){ return a.ratio>b.ratio;// 按照比率从大到小排序 } int main(){ vector<Item> items={ Item(10,60), Item(20,100), Item(30,120) }; sort(items.begin(),items.end(),compare); // 对物品进行排序 cout<<"Sorted Items by Value-to-Weight Ratio:"<<endl; for(auto item:items){ printf("Value:%f Weight:%f Ratio:%f\n",item.value,item.weight,item.ratio); } return 0; } ``` 这里实现了简单的物品分类依据其单位质量所含有的经济利益来进行初步调整位置从而方便下一步骤的操作流程安排. ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值