#include
#include
int Binarysearch(int w[],int n,int x) {
int m,high = n - 1,low = 0;
while(low <= high) {
m = (low + high)/2;
if(x == w[m]) return m;
else if(x > w[m]) high = m - 1;
else low = m + 1;
}
return -1;
}
int main() {
int i,j,n,t,index,scr;
char name[1000][23],tmp[23];
int score[1000];
printf("输入要统计的学生个数:");
scanf("%d",&n);
for(i = 0;i < n;++i) {
printf("姓名 成绩:");
scanf("%s%d",name[i],&score[i]);
}
for(i = 0;i < n - 1;i++) {
for(j = 0;j < n - i - 1 ;j++) {
if(score[j] < score[j + 1]) {
t = score[j];
score[j] = score[j + 1];
score[j + 1] = t;
strcpy(tmp,name[j]);
strcpy(name[j],name[j + 1]);
strcpy(name[j + 1],tmp);
}
}
}
printf("学生姓名和C语言课程的成绩从高到低输出:\n");
for(i = 0;i < n;++i)
printf("%s\t%d\n",name[i],score[i]);
printf("\n输入要查找的成绩:");
scanf("%d",&scr);
index = Binarysearch(score,n,scr);
if(index >= 0) printf("%s\t%d\n",name[index],score[index]);
return 0;
}