# 对于语文和学号的排序 我写了两段不一样的代码 单独使用一个都会WA 两个都用才能AC 为什么呢

``````#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
struct stu {
int id;
int a;
int b;
int c;
int abc;
};
void swap (struct stu *a, struct stu *b)
{
struct stu tmp;
tmp=*a;
*a=*b;
*b=tmp;
}
void kuaipai(struct stu *data,int a,int b)
{
int i,j=0,k=b-a;
if(b-a<=0)
return;
if(b-a<=1)
{
if(data[a].abc<data[b].abc)
{
swap(&data[a],&data[b]);
}
}
else
{
struct stu *tmp = calloc(b-a+1, sizeof(struct stu)); //int tmp[b-a+1]; memset(tmp,0,sizeof(tmp));
for(i=a+1;i<=b;i++)
{
if(data[i].abc >data[a].abc )
{
tmp[j]=data[i];
j++;
}
if(data[i].abc <data[a].abc)
{
tmp[k]=data[i];
k--;
}
if(data[i].abc==data[a].abc)
{
if(data[i].a>data[a].a)
{
tmp[j]=data[i];
j++;
}
if(data[i].a<data[a].a)
{
tmp[k]=data[i];
k--;
}
if(data[i].a==data[a].a)
{
if(data[i].id>data[a].id)
{
tmp[j]=data[i];
j++;
}
if(data[i].id<data[a].id)
{
tmp[k]=data[i];
k--;
}
}
}
}j--;k++;tmp[j+1]=data[a];
for(i=0;i<b-a+1;i++)
{
data[a+i]=tmp[i];
}
free(tmp);
kuaipai(data,a,a+j);
kuaipai(data,a+k,b);
}
}
int main()
{

int i,n;
scanf("%d",&n);
struct stu chengji[n+1]; //memset(W1,0,sizeof(W1));
for(i=1;i<=n;i++)
scanf("%d %d %d",&chengji[i].a,&chengji[i].b,&chengji[i].c);
for(i=1;i<=n;i++)
{
chengji[i].abc=chengji[i].a+chengji[i].b+chengji[i].c;
chengji[i].id=i;
}
kuaipai(chengji,1,n);
/*  for(i=1;i<=n-1;i++)
{
int j;
for(j=i;j<=n;j++)
{
if(chengji[i].abc !=chengji[j].abc) break;
}j--;
int k,l;
for(k=i;k<=j;k++)
{
for(l=k;l<=j;l++)
if(chengji[k].a<chengji[l].a)
swap(&chengji[k],&chengji[l]);
}
i=j;
}*/
/*for(i=1;i<=n-1;i++)
{   int j;
for(j=i;j<=n;j++)
{
if(chengji[i].abc !=chengji[j].abc||chengji[i].a!=chengji[j].a) break;
}j--;
int k,l;
for(k=i;k<=j;k++)
{
for(l=k;l<=j;l++)
if(chengji[k].id>chengji[l].id)
swap(&chengji[k],&chengji[l]);
}
i=j;
}*/
for(i=1;i<=5;i++)
printf("%d %d\n",chengji[i].id,chengji[i].abc );

}
``````

``````#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
struct stu {
int id;
int a;
int b;
int c;
int abc;
};
void swap (struct stu *a, struct stu *b)
{
struct stu tmp;
tmp=*a;
*a=*b;
*b=tmp;
}
void kuaipai(struct stu *data,int a,int b)
{
int i,j=0,k=b-a;
if(b-a<=0)
return;
if(b-a<=1)
{
if(data[a].abc<data[b].abc)
{
swap(&data[a],&data[b]);
}
}
else
{
struct stu *tmp = calloc(b-a+1, sizeof(struct stu)); //int tmp[b-a+1]; memset(tmp,0,sizeof(tmp));
for(i=a+1;i<=b;i++)
{
if(data[i].abc >data[a].abc )
{
tmp[j]=data[i];
j++;
}
if(data[i].abc <data[a].abc)
{
tmp[k]=data[i];
k--;
}
if(data[i].abc==data[a].abc)
{
if(data[i].a>data[a].a)
{
tmp[j]=data[i];
j++;
}
if(data[i].a<data[a].a)
{
tmp[k]=data[i];
k--;
}
/*if(data[i].a==data[a].a)
{
if(data[i].id>data[a].id)
{
tmp[j]=data[i];
j++;
}
if(data[i].id<data[a].id)
{
tmp[k]=data[i];
k--;
}
}*/
}
}j--;k++;tmp[j+1]=data[a];
for(i=0;i<b-a+1;i++)
{
data[a+i]=tmp[i];
}
free(tmp);
kuaipai(data,a,a+j);
kuaipai(data,a+k,b);
}
}
int main()
{

int i,n;
scanf("%d",&n);
struct stu chengji[n+1]; //memset(W1,0,sizeof(W1));
for(i=1;i<=n;i++)
scanf("%d %d %d",&chengji[i].a,&chengji[i].b,&chengji[i].c);
for(i=1;i<=n;i++)
{
chengji[i].abc=chengji[i].a+chengji[i].b+chengji[i].c;
chengji[i].id=i;
}
kuaipai(chengji,1,n);
/*  for(i=1;i<=n-1;i++)
{
int j;
for(j=i;j<=n;j++)
{
if(chengji[i].abc !=chengji[j].abc) break;
}j--;
int k,l;
for(k=i;k<=j;k++)
{
for(l=k;l<=j;l++)
if(chengji[k].a<chengji[l].a)
swap(&chengji[k],&chengji[l]);
}
i=j;
}*/
for(i=1;i<=n-1;i++)
{   int j;
for(j=i;j<=n;j++)
{
if(chengji[i].abc !=chengji[j].abc||chengji[i].a!=chengji[j].a) break;
}j--;
int k,l;
for(k=i;k<=j;k++)
{
for(l=k;l<=j;l++)
if(chengji[k].id>chengji[l].id)
swap(&chengji[k],&chengji[l]);
}
i=j;
}
for(i=1;i<=5;i++)
printf("%d %d\n",chengji[i].id,chengji[i].abc );

}
``````

# 3 条评论

• @ 2019-02-09 15:27:04

我记得是有一个优先级的
还有——用STL不好吗？

• @ 2019-02-02 21:48:13

指针蛮炫吗，您没发现用ｓｔｌ您的代码可以缩短很多倍吗？
请用ｓｏｒｔ再自己打ｃｏｍｐ

• @ 2019-02-02 09:50:13

这个是可以AC的

``````这个是可以AC的
```cpp
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
struct stu {
int id;
int a;
int b;
int c;
int abc;
};
void swap (struct stu *a, struct stu *b)
{
struct stu tmp;
tmp=*a;
*a=*b;
*b=tmp;
}
void kuaipai(struct stu *data,int a,int b)
{
int i,j=0,k=b-a;
if(b-a<=0)
return;
if(b-a<=1)
{
if(data[a].abc<data[b].abc)
{
swap(&data[a],&data[b]);
}
}
else
{
struct stu *tmp = calloc(b-a+1, sizeof(struct stu)); //int tmp[b-a+1]; memset(tmp,0,sizeof(tmp));
for(i=a+1;i<=b;i++)
{
if(data[i].abc >data[a].abc )
{
tmp[j]=data[i];
j++;
}
if(data[i].abc <data[a].abc)
{
tmp[k]=data[i];
k--;
}
if(data[i].abc==data[a].abc)
{
if(data[i].a>data[a].a)
{
tmp[j]=data[i];
j++;
}
if(data[i].a<data[a].a)
{
tmp[k]=data[i];
k--;
}
if(data[i].a==data[a].a)
{
if(data[i].id>data[a].id)
{
tmp[j]=data[i];
j++;
}
if(data[i].id<data[a].id)
{
tmp[k]=data[i];
k--;
}
}
}
}j--;k++;tmp[j+1]=data[a];
for(i=0;i<b-a+1;i++)
{
data[a+i]=tmp[i];
}
free(tmp);
kuaipai(data,a,a+j);
kuaipai(data,a+k,b);
}
}
int main()
{

int i,n;
scanf("%d",&n);
struct stu chengji[n+1]; //memset(W1,0,sizeof(W1));
for(i=1;i<=n;i++)
scanf("%d %d %d",&chengji[i].a,&chengji[i].b,&chengji[i].c);
for(i=1;i<=n;i++)
{
chengji[i].abc=chengji[i].a+chengji[i].b+chengji[i].c;
chengji[i].id=i;
}
kuaipai(chengji,1,n);
/*  for(i=1;i<=n-1;i++)
{
int j;
for(j=i;j<=n;j++)
{
if(chengji[i].abc !=chengji[j].abc) break;
}j--;
int k,l;
for(k=i;k<=j;k++)
{
for(l=k;l<=j;l++)
if(chengji[k].a<chengji[l].a)
swap(&chengji[k],&chengji[l]);
}
i=j;
}*/
for(i=1;i<=n-1;i++)
{   int j;
for(j=i;j<=n;j++)
{
if(chengji[i].abc !=chengji[j].abc||chengji[i].a!=chengji[j].a) break;
}j--;
int k,l;
for(k=i;k<=j;k++)
{
for(l=k;l<=j;l++)
if(chengji[k].id>chengji[l].id)
swap(&chengji[k],&chengji[l]);
}
i=j;
}
for(i=1;i<=5;i++)
printf("%d %d\n",chengji[i].id,chengji[i].abc );

}```

``````
• 1

ID
1398

4

6167

2538

41%