1760: 学生信息查找

#include<stdio.h>
struct student
{
long no;
char name[9];
int score;
} ;
void input(struct student stu[],int n)
{
int j;
for(j=0;j<n;j++)
{
scanf("%ld %s %d",&stu[j].no,&stu[j].name,&stu[j].score);
}
}
int search(struct student stu[],int n,long xh)
{
int j,flag=1;
for(j=0;j<n;j++)
{
if(stu[j].no==xh)
{
return j;
flag=0;
break;
}
}
if(flag) return -1;
}
void print(struct student stu[],int t)
{
printf("%ld %s %d\n",stu[t].no,stu[t].name,stu[t].score);
}
int main()
{
int i,n,m,t;
long xh;
struct student stu[100];
while(scanf("%d",&n)!=EOF)
{
input(stu,n); //读入n个学生的数据
scanf("%d",&m);
for(i=0;i<m;i++)
{
scanf("%ld",&xh);
t=search(stu,n,xh); //查找学号为xh的学生,不存在返回-1,存在则返回其下标
if(t==-1)
printf("%d not exist\n",xh);
else
print(stu,t); //输出第t个学生的信息
}
}
return 0;
}

猜你喜欢

转载自www.cnblogs.com/cy846586184/p/12288463.html