Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

谁能帮看下,qsort()对指针排序让人心碎!

Posted by orlando22 at 2009-04-18 13:52:31 on Problem 1639
结果是用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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator