本帖最后由 jhq999 于 2021-12-9 07:03 编辑 typedef struct STUDENT
{
int id;
char name[16];
int *achievement;
float average;
float sum;
}Student,pStudent;
int main(void) {
int i=0,j=0,M=0,N=0;//M学生数量,N科目数量
scanf("%d%d",&M,&N);
Student *a=new Student[M];
int *ptmp=new int[M*N];
for(i=0;i<M;i++)
{
//a[i].id=i;
scanf("%d",&a[i].id);
scanf("%s",a[i].name);
a[i].achievement=ptmp+i*N;
a[i].sum=0;
a[i].average=0;
for(j=0;j<N;j++)
{
scanf("%d",&a[i].achievement[j]);
a[i].sum+=a[i].achievement[j];
}
a[i].average=a[i].sum/N;
// printf("%.1f ",a[i].average);
}
//printf("\n");
/*Student stmp={0};
int flag=1;
for(i=0;i<M-1;i++)
{
flag=1;
for(j=1;j<M-i;j++)
{
if(a[j-1].sum<a[j].sum)
{
flag=0;
stmp=a[j-1],a[j-1]=a[j],a[j]=stmp;
}
}
if(flag)break;
}
i=0;
while(a[i].sum==a[0].sum&&i<N)
{
printf("%d ",a[i].id+1);
i++;
}
printf("\n");
for(i=0;i<M;i++)printf("%d ",a[i].id+1);*/
for(i=0;i<M;i++)
{
printf("%d %s ",a[i].id,a[i].name);
for(j=0;j<N;j++)printf("%d ",a[i].achievement[j]);
printf("%.1f\n",a[i].average);
}
delete[] ptmp;
delete[] a;
return 0;
}
4 3
1 abc 50 60 75
2 bcd 90 100 81
3 cde 40 80 99
4 def 88 90 53
1 abc 50 60 75 61.7
2 bcd 90 100 81 90.3
3 cde 40 80 99 73.0
4 def 88 90 53 77.0
|