中文域名网站有哪些,个人商城网站源码,外贸网站建设的重要性,如何建立自己的博客网站这篇文章是 一楼那个学长写的 C库函数qsort七种使用方法示例 七种qsort排序方法本文中排序都是采用的从小到大排序 一、对int类型数组排序C代码 int num[100]; Sample: int cmp ( const void *a , const void *b ) { return *(int *)a – *(int *)b; } q…这篇文章是 一楼那个学长写的 C库函数qsort七种使用方法示例 七种qsort排序方法本文中排序都是采用的从小到大排序 一、对int类型数组排序C代码 int num[100]; Sample: int cmp ( const void *a , const void *b ) { return *(int *)a – *(int *)b; } qsort(num,100,sizeof(num[0]),cmp); 二、对char类型数组排序同int类型C代码 char word[100]; Sample: int cmp( const void *a , const void *b ) { return *(char *)a – *(char*)b; } qsort(word,100,sizeof(word[0]),cmp) 三、对double类型数组排序特别要注意C代码 double in[100]; int cmp( const void *a , const void *b ) { return *(double *)a *(double *)b ? 1 : -1; } qsort(in,100,sizeof(in[0]),cmp) 四、对结构体一级排序C代码 struct In { double data; int other; }s[100] //按照data的值从小到大将结构体排序,关于结构体内的排序关键数据data的类型可以很多种参考上面的例子写 int cmp( const void *a ,const void *b) { return (*(In *)a).data (*(In *)b).data ? 1 : -1; } qsort(s,100,sizeof(s[0]),cmp); 五、对结构体二级排序C代码 struct In { int x; int y; }s[100]; //按照x从小到大排序当x相等时按照y从大到小排序 int cmp( const void *a , const void *b ) { struct In *c (In *)a; struct In *d (In *)b; if(c-x ! d-x) return c-x – d-x; else return d-y – c-y; } qsort(s,100,sizeof(s[0]),cmp); 六、对字符串进行排序C代码 struct In { int data; char str[100]; }s[100]; //按照结构体中字符串str的字典顺序排序 int cmp ( const void *a , const void *b ) { return strcmp( (*(In *)a)-str , (*(In *)b)-str ); } qsort(s,100,sizeof(s[0]),cmp); 七、计算几何中求凸包的cmpC代码 int cmp(const void *a,const void *b) //重点cmp函数把除了1点外的所有点旋转角度排序 { struct point *c(point *)a; struct point *d(point *)b; if( calc(*c,*d,p[1]) 0) return 1; else if( !calc(*c,*d,p[1]) dis(c-x,c-y,p[1].x,p[1].y) dis(d-x,d-y,p[1].x,p[1].y)) //如果在一条直线上则把远的放在前面 return 1; else return -1; } PS: 其中的qsort函数包含在的头文件里strcmp包含在的头文件里 大家自己先做下不要搜代码 NYOJ 7(街区最短路经) #includestdio.h #includestdlib.h #includemath.h int x[101],y[101]; int x2[101],y2[101]; int cmp(const void*a,const void*b) { return *(int *)a-*(int *)b; } int main() { int N; while(scanf(%d,N)!EOF) { while(N--) { int i,n,sum0; scanf(%d,n); for(i0;in;i) { scanf(%d %d,x[i],y[i]); x2[i]x[i]; y2[i]y[i]; } qsort(x2,n,sizeof(x2[0]),cmp); qsort(y2,n,sizeof(y2[0]),cmp); for(i0;in;i) { if((x2[n/2]x[i])(y2[n/2]y[i])) { continue; } sumabs(x[i]-x2[n/2])abs(y[i]-y2[n/2]); } printf(%d\n,sum); } } return 0; } NYOJ 540(奇怪的排序) 来自第五届省赛简单题 #includestdio.h #includestdlib.h typedef struct Node { int mmm;// 数本身 int mm;// 反序后的数 }Node; Node node[55]; int cmp(const void *a,const void *b) { Node *c (Node *)a; Node *d (Node *)b; return c-mm - d-mm; } int main() { int N,m,n,i,x,k; scanf(%d,N); while(N--) { x0; scanf(%d %d,m,n); for(im;in;i) { x; ki; node[x].mmmi; node[x].mm0; while(k) { node[x].mmnode[x].mm*10k%10; kk/10; } } qsort(node1,n-m1,sizeof(node[1]),cmp); printf(%d,node[1].mmm); for(i2;in-m1;i) { printf( %d,node[i].mmm); } printf(\n); } return 0; } import java.util.Arrays; import java.util.Scanner; public class NYOJ540 { public static void main(String[] args) { int from, to, T; Node node[]; Scanner sc new Scanner(System.in); T sc.nextInt(); int temp; while(T--0) { from sc.nextInt(); to sc.nextInt(); node new Node[to-from1]; int j 0; for(int i0; inode.length; i) { //q已经初始化为0了 node[i] new Node(); } for(int ifrom; ito; i) { node[j].p i; temp i; while(temp0) { /* * 必须在大while循环构造node数组 * 否则就第一组数据正确 * 因为下面这一句用到了以前的q值 */ node[j].q node[j].q*10 temp%10; temp / 10; } j; } /* * 只看API函数第三个参数是toIndex以为是下标 * 谁知道具体一看不包括wa了n次 */ Arrays.sort(node,0,to-from1); System.out.print(node[0].p); for(int i1; ito-from; i) { System.out.print( node[i].p); } System.out.println( node[to-from].p); } } } class Node implements ComparableNode{ int p; int q; public Node() { this.p 0; this.q 0; } Override public int compareTo(Node o) { // TODO Auto-generated method stub Node other o; return this.q - other.q; } }转载于:https://www.cnblogs.com/hpuzyf/p/3237730.html