#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct tag_stdata {
int student;
int score;
} stdata;
void grow(stdata** pst, int n, int* psz)
{
if (n < *psz) return;
*psz += 10;
if (*pst == NULL) {
*pst = (stdata*)malloc((*psz) * sizeof(stdata));
}
else {
*pst = (stdata*)realloc(*pst, (*psz) * sizeof(stdata));
}
if (*pst == NULL) exit(1);
}
int cmp(const void* x, const void* y)
{
return ((stdata*)x)->score - ((stdata*)y)->score;
}
int main()
{
char s[80];
stdata* st = NULL;
int stsize, count, i;
stsize = count = 0;
for (;;) {
printf("student[%d]:", count+1);
if (gets(s) == NULL) break;
if (strlen(s) == 0) break;
grow(&st, count, &stsize);
st[count].student = count + 1;
st[count].score = atoi(s);
++count;
}
qsort((void*)st, count, sizeof(stdata), cmp);
for (i = 0; i < count; i++) {
printf("student[%d]:%d\n", st[i].student, st[i].score);
}
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPHN0cmluZy5oPgoKdHlwZWRlZiBzdHJ1Y3QgdGFnX3N0ZGF0YSB7CglpbnQgc3R1ZGVudDsKCWludCBzY29yZTsKfSBzdGRhdGE7Cgp2b2lkIGdyb3coc3RkYXRhKiogcHN0LCBpbnQgbiwgaW50KiBwc3opCnsKCWlmIChuIDwgKnBzeikgcmV0dXJuOwoJKnBzeiArPSAxMDsKCWlmICgqcHN0ID09ICBOVUxMKSB7CgkJKnBzdCA9IChzdGRhdGEqKW1hbGxvYygoKnBzeikgKiBzaXplb2Yoc3RkYXRhKSk7Cgl9CgllbHNlIHsKCQkqcHN0ID0gKHN0ZGF0YSopcmVhbGxvYygqcHN0LCAoKnBzeikgKiBzaXplb2Yoc3RkYXRhKSk7Cgl9CglpZiAoKnBzdCA9PSBOVUxMKSBleGl0KDEpOwp9CgppbnQgY21wKGNvbnN0IHZvaWQqIHgsIGNvbnN0IHZvaWQqIHkpCnsKCXJldHVybiAoKHN0ZGF0YSopeCktP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