1 1 1 1.1
1 1 3 1.2
2 1 3 2.9
1 2 1 1.2
2 1 2 2.8
1 2 2 1.5
2 2 2 3.1
1 2 3 2.3
2 2 1 3.0
1 1 2 2.5
1 2 4 2.4
1 3 1 2.6
2 1 1 2.7
2 2 3 3.2

1 1 1 1.1
1 1 2 2.5
1 1 3 1.2
1 2 1 1.2
1 2 2 1.5
1 2 3 2.3
1 2 4 2.4
1 3 1 2.6
2 1 1 2.7
2 1 2 2.8
2 1 3 2.9
2 2 1 3.0
2 2 2 3.1
2 2 3 3.2

0

1个回答

qsort对结构体进行快速排序，直接上代码如下：

`````` #include<stdio.h>
#include<stdlib.h>

struct item
{
int a;
int b;
int c;
double d;
};

int cmp(const void *a, const void*b)
{
struct item *c = (struct item *)a;
struct item *d = (struct item *)b;
if(c->a != d->a)
return c->a - d->a;
else if(c->b != d->b)
return c->b - d->b;
else if(c->c != d->c)
return c->c - d->c;
else
return c->d < d->d;
}

int main()
{
int n,i,a,b,c;
double d;
struct item m[100];
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d%d%d%lf",&a,&b,&c,&d);
m[i].a = a;
m[i].b = b;
m[i].c = c;
m[i].d = d;
}

qsort(m,n,sizeof(m[0]),cmp);

printf("排序后：\n");
for(i=0;i<n;i++)
printf("%d %d %d %.1lf\n",m[i].a,m[i].b,m[i].c,m[i].d);

system("pause");
return 0;
}
``````

1