Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
谁能帮看下,qsort()对指针排序让人心碎!结果是用sort, push_heap,自己的代码写的! #include <time.h> #include <stdlib.h> using namespace std; const int MAXN=22, MAXE=500; typedef struct Edge{ int i, j, value; }Edge, *Pedge; Edge storage[MAXE]; Pedge links[MAXE];//for heapsorting int eid=0,lsize=0; int cmp(const void* p1, const void* p2){ return (*(Pedge*)p1)->value>(*(Pedge*)p1)->value; } int _tmain(int argc, _TCHAR* argv[]) { srand((unsigned int)time(0)); int min=1, max=400; for(int i=0; i<400; i++){ storage[eid].i=i, storage[eid].j=i-1, storage[eid].value=(double)rand()/(1+RAND_MAX)*(max-min)+min; links[lsize++]=storage+(eid++); } qsort((void*)links, lsize, sizeof(Pedge*), cmp); for(int i=1; i<lsize; i++){ cout<<links[i]->value<<" "; assert(links[i]->value>=links[i-1]->value);//出错?排的让人莫名其妙,Horae的算法应该不会有错把! } system("pause"); return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator