你哪来这么多事(二):学生信息查找
學生信息查找
Time Limit: 1 Sec??Memory Limit: 128 MB?? 64bit IO Format: %lld
Description
某班 有n個學生(不超過100),每個學生的信息包括學號(long no),姓名(char name[9])和成績(int score)。
編寫程序,先將n個學生的信息錄入到結構體數(shù)組中(n個學生的數(shù)據(jù)已按學號升序有序),
然后需要查詢m個學生是否在本班上(按學號查找),如果在的話,輸出其詳細信息,否則輸出不存在。
注意:main函數(shù)已經(jīng)給定(如下所示)。
? ? ? ? ? ?請將程序補充完整。
? ? ? ? ? ?提交時只需要提交自己補充的代碼部分,不需要提交給定的main函數(shù)的代碼部分。
#include<stdio.h>
int main()
{int i,n,m,t;long xh;struct student stu[100];while(scanf("%d",&n)!=EOF){input(stu,n); //讀入n個學生的數(shù)據(jù)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);elseprint(stu,t); //輸出第t個學生的信息}}return 0;
}
Input
包含多組測試數(shù)據(jù),每組測試數(shù)據(jù)第一行包含1個正整數(shù)n,表示學生人數(shù)。
接下來的n行,每行為1個學生的詳細信息(已按學號升序有序),包括學號,姓名和成績,各數(shù)據(jù)之間用空格隔開。
接下來的一行包括一個正整數(shù)m,表示待查找的學生人數(shù)。
最后是m行,每行1個整數(shù),表示帶查找學生的學號。
其中,n<100,m<n。
?
Output
每組測試數(shù)據(jù)輸出占1行,如果不存在則輸出“*** not exist”(***為查找的學號),如果存在,則輸出學生的信息,數(shù)據(jù)依次為:學號,姓名,成績。各部分數(shù)據(jù)之間用空格隔開。(具體詳見樣例輸出)
?
Sample Input
3
1001 aaa 86
1002 bbb 78
1003 ccc 70
2
1004
1001
?
Sample Output
1004 not exist
1001 aaa 86
題目分析:
會做上一題了就會做這一題。
?
搜索這個學號,搜到了則將這個學號的學生信息輸出。
?
以下僅為答案部分代碼~
最后:抄代碼有害無益喲~
struct student
{long no;char name[9];int score;
};
void input(struct student stu[100],int n)
{int i;for(i=1;i<=n;i++){scanf("%ld %s %d",&stu[i].no,stu[i].name,&stu[i].score);}return;
}
int search(struct student stu[100],int n,long xh)
{int i;for(i=1;i<=n;i++)if(stu[i].no==xh)return i;return -1;
}//人類的本質是什么
void print(struct student stu[100],int t)
{printf("%ld %s %d\n",stu[t].no,stu[t].name,stu[t].score);return;
}
?
總結
以上是生活随笔為你收集整理的你哪来这么多事(二):学生信息查找的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 求作开头的成语接龙!
- 下一篇: 你哪来这么多事(三):学生信息删除