回复 2楼 止步于未来
#include <stdio.h>
#define N 30
/*宏常量:以最大范围定义,便于数据改动及避免数据出界*/
void Readscore(long ID[],int score[],int n);
/*Readscore()函数原型*/
void SortID(long ID[],int score[],int n);
int Finddate(long ID[],long x,int n);
int main()
{
int score[N],n,mid;
long ID[N],x;
/*以长整型接收学号数据*/
printf("Input n:\n");
scanf("%d",&n);
Readscore(ID,score,n);
/*数组名作为函数实参调用函数 Readscore() */
SortID(ID,score,n);
printf("Input your aim ID:\n");
scanf("%ld",&x);
mid=Finddata(ID,x,n);
if(mid==-1)
{
printf("No student %ld",x);
}
else
printf("Ranking is:%d Score is:%d",mid,score[mid]);
return 0;
}
void Readscore(long ID[],int score[],int n)
/*函数功能:读入学生成绩数据和学号*/
{
printf("Please pay attention to your date:score>0 and ID should be different\n"); /*提示用户输入合法数据*/
int i;
for(i=0;i<n;i++)
{
scanf("%ld %d",&ID[i],&score[i]);
}
}
void SortID(long ID[],int score[],int n)
/*函数功能:排序成绩*/
{
int i,j,k,agent;
long sagent;
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(ID[j]<ID[i])
/*按学号的升序排序*/
{
agent=score[j];
score[j]=score[i];
score[i]=agent;
sagent=ID[j];
ID[j]=ID[i];
ID[i]=sagent;
}
}
}
}
int Finddate(long ID[],long x,int n)
{
int low=0,high=n-1,mid;
while(low<=high)
{
mid=(high+low)/2;
if(x>ID[mid])
low=mid+1;
else if (x<ID[mid])
high=mid-1;
else
return mid;
}
return -1;
}
源代码是这个,求大佬帮看啊
实在找不到