#include <stdio.h>
typedef struct{
int id; //学籍番号
int mth; //数学
int eng; //英語
int phy; //物理
int avg; //平均点
} Score;
void calcAverage(Score s[], int n);
void showScore(Score s[], int n );
void swap(Score *x, Score *y);
int main(void) {
Score s[] = {
{ 1001, 65, 80, 95, 0},
{ 1002, 70, 68, 75, 0},
{ 1003, 60, 100, 83, 0},
{ 1004, 100, 55, 74, 0},
{ 1005, 90, 85, 100, 0},
};
int i,j, n=5;
calcAverage(s, n);
showScore(s, n);
for(i=0;i<n; i++)
for(j=n-1; j>i; j--)
if( s[j-1].avg>s[j].avg )
swap( &s[j-1], &s[j] );
printf( "\nAfter sorting\n" ); showScore(s, n);
return 0;
}
void calcAverage(Score s[], int n){
int i;
for( i=0; i<n; i++ )
s[i].avg = (s[i].mth + s[i].eng + s[i].phy)/3;
}
void showScore(Score s[], int n ){
int i;
for(i=0; i<n; i++ )
printf( "[%5d] %4d %4d %4d %4d\n", s
[i
].
id, s
[i
].
mth, s
[i
].
eng, s
[i
].
phy, s
[i
].
avg ); }
void swap( Score *x, Score *y ){
Score tmp=*x;
*x = *y;
*y = tmp;
}
I2luY2x1ZGUgPHN0ZGlvLmg+Cgp0eXBlZGVmIHN0cnVjdHsKCWludCBpZDsgICAgLy/lrabnsY3nlarlj7cKCWludCBtdGg7IC8v5pWw5a2mCglpbnQgZW5nOyAvL+iLseiqngoJaW50IHBoeTsgLy/niannkIYKCWludCBhdmc7IC8v5bmz5Z2H54K5Cn0gU2NvcmU7IAoKdm9pZCBjYWxjQXZlcmFnZShTY29yZSBzW10sIGludCBuKTsKdm9pZCBzaG93U2NvcmUoU2NvcmUgc1tdLCBpbnQgbiApOwp2b2lkIHN3YXAoU2NvcmUgKngsIFNjb3JlICp5KTsKCmludCBtYWluKHZvaWQpIHsKCVNjb3JlIHNbXSA9IHsKCQl7IDEwMDEsICA2NSwgIDgwLCAgOTUsIDB9LAoJCXsgMTAwMiwgIDcwLCAgNjgsICA3NSwgMH0sCgkJeyAxMDAzLCAgN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