//第一题
#include <stdio.h>
#include <stdlib.h>
typedef struct pet{
int id;
char name[20];
int score[3];
int sum;
}PET;
int main()
{
int i,j,N;
PET *pet,tmp;
scanf("%d",&N);
pet=(PET*)malloc(N*sizeof(PET));
for(i=0;i<N;i++){
scanf("%d%s%d%d%d",&pet[i].id,pet[i].name,&pet[i].score[0],&pet[i].score[1],&pet[i].score[2]);
pet[i].sum=pet[i].score[0]+pet[i].score[1]+pet[i].score[2];
}
for(i=0;i<N-1;i++)
for(j=0;j<N-i-1;j++)
if(pet[j].sum < pet[j+1].sum){
tmp=pet[j];
pet[j]=pet[j+1];
pet[j+1]=tmp;
}
for(i=0;i<3;i++)
{
printf("%d %s %d %d %d %d\n",pet[i].id,pet[i].name,pet[i].score[0],pet[i].score[1],pet[i].score[2],pet[i].sum);
}
free(pet); //释放动态内存空间
return 0;
}
//示例运行结果
F:\c_work>a.exe
5
1000 aaa 12 23 56
1001 bbb 66 85 63
1002 ccc 55 88 36
1003 ddd 78 75 76
1004 eee 85 66 72
1003 ddd 78 75 76 229
1004 eee 85 66 72 223
1001 bbb 66 85 63 214
//第2题
#include <stdio.h>
#include <stdlib.h>
//冒泡排序(降序)
void bubble_sort(int a[],int n)
{
int i,j; //j表示趟数,i表示第j趟两两比较的次数
int tmp; //临时变量
for(i=0;i<n-1;i++)
for(j=0;j<n-1-i;j++)
{
if(a[j] < a[j+1])
{
tmp=a[j];
a[j]=a[j+1];
a[j+1]=tmp;
}
}
}
int main()
{
int T,n;
int i,j;
int **ps;
int *pn;
scanf("%d",&T);
pn=(int*)malloc(T*sizeof(int)); //用来存放每组测试数据的个数
ps=(int**)malloc(T*sizeof(int*)); //动态分配T个int*指针内存空间
for(i=0;i<T;i++){
scanf("%d",&n);
pn[i]=n;
ps[i]=(int*)malloc(n*sizeof(int)); //动态分配n个int内存空间存放每组的数据
for(j=0;j<n;j++)
scanf("%d",ps[i]+j);
bubble_sort(ps[i],n);
}
for(i=0;i<T;i++)
for(j=0;j<pn[i];j++)
{
if(j == pn[i]-1)
printf("%d\n",ps[i][j]);
else
printf("%d ",ps[i][j]);
}
//释放动态内存空间
for(i=0;i<T;i++)
free(ps[i]);
free(ps);
free(pn);
return 0;
}
//示例运行结果
F:\c_work>a.exe
2
3 670 980 230
4 90 87 76 960
980 670 230
960 90 87 76
温馨提示:答案为网友推荐,仅供参考