//将数组A:1~n,随机重排
#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
const int N=100;
using namespace std;
struct FOO{
int priority;int initial_index;
bool operator <(const FOO&another)const{
return priority<another.priority;
}
}P[N];
int A[N];
int main(void)
{
int n=9;
for(int i=1;i<=n;i++)//A[i]赋值一个随机的优先级P[i],再按优先级重排
{P[i].priority=rand()%n*n*n+1;P[i].initial_index=i;}
sort(P+1,P+n+1);
//for(int i=1;i<=n;i++) printf("%d %d\n",P[i].priority,P[i].initial_index);
for(int i=1;i<=n;i++) A[i]=P[i].initial_index;
for(int i=1;i<=n;i++) printf("%d ",A[i]);
return 0;
}
permute by sorting
最新推荐文章于 2021-12-01 16:20:17 发布