雇佣问题随机排列数组(permuteBySorting)-c++代码实现
来源:互联网 发布:守望先锋设置优化 编辑:程序博客网 时间:2024/05/17 18:25
为数组中A每个元素随机赋一个优先级,然后依据优先级P对数组进行排序,详细见算法导论第三版或者网上
伪代码
c++代码
#include <iostream>#include<time.h>#define N 20//数组大小为20void randP(int prior[],int len);void permuteBySorting(int array[],int prior[],int len);using namespace std;int main(){ int Array[N],Prior[N]; for(int i=0;i<N;++i)//数组Array循环赋初值 Array[i]=i+1; cout<<"原数组Array的值为:"<<endl; for(int j=0;j<N;++j) cout<<Array[j]<<" "; cout<<endl<<"与数组对应的优先级大小为:"<<endl; randP(Prior,N); permuteBySorting(Array,Prior,N); cout<<endl<<"排序后的数组Array为:"<<endl; for(int i=0;i<N;++i) cout<<Array[i]<<" "; cout<<endl; return 0;}void randP(int prior[],int len)//随机生成优先级{ srand(time(NULL));//rand随机数计算之前需要设置随机种子,不然每次结果一样 for(int i=0;i<len;++i) { prior[i]=rand()%(len*len*len);//取值大尽可能让优先级唯一 cout<<prior[i]<<" "; }}void permuteBySorting(int array[],int prior[],int len)//排序{ int temp1=0;//初始最小 int temp2=0; for(int i=1;i<len;++i) { temp1=prior[i]; temp2=array[i]; int j; for(j=i-1;(j>=0&&prior[j]>temp1);--j) { prior[j+1]=prior[j]; array[j+1]=array[j]; } prior[j+1]=temp1; array[j+1]=temp2; }}
第一次运行结果
第二次运行结果
阅读全文
0 0
- 雇佣问题随机排列数组(permuteBySorting)-c++代码实现
- 雇佣问题原址排列给定数组(randomize In Place)-c++代码实现
- 雇佣问题(hireassistant)-c++代码实现
- 在线雇佣问题(onlineMaximumHireAssistant)-c++代码实现
- 随机排列数组(二)
- java实现数组随机重新排列
- c语言实现随机排列n个数
- java随机排列数组
- 随机排列数组
- 原地随机排列数组
- 随机排列数组
- 随机排列数组
- 随机排列数组
- 随机排列数组
- 随机排列数组
- 数组中的数据随机排列的生成代码及方法
- 雇佣问题
- 雇佣问题
- 算法之插入排序(C++实现)
- python数据结构及实例
- apache一键安装,MSI文件
- httpClient入门到精通-------连接池
- [Unity教程]如何暂停
- 雇佣问题随机排列数组(permuteBySorting)-c++代码实现
- 自动化运维工具-Puppet
- Oracle自增长
- Vue 2.0在IE11中打开项目页面空白的问题
- poj2289 Jamie's Contact Groups(二分答案+最大流)
- 内网渗透使用代理访问内网
- PHP数组函数总结
- spring-Mybatis整合
- linux进程间通信--消息队列相关函数(msgget、msgctl、msgsnd、msgrcv)详解