c语言常用的查找算法-顺序查找

顺序查找:
依次遍历查找数据

#include "stdio.h"
typedef struct student{
     int id;    //编号
     char name[10]; //姓名
     float score;   //分数
}std;

int search(std s[],int n,int key){
     int i;
     for(i=0;i < n;i++)
         if(s[i].id == key)
            return i;
    return -1;
}

int main(){
       std s[]={
         {1,"张三",98},
         {2,"吴曦月",92},
         {3,"小明",89},
         {4,"关悦",96}
        };

       int t=-1;
       int y=0;
       printf("请输入需要获取的学生编号\n");
       scanf("%d",&y);
       t = search(s,4,y);
       if(t == -1)
         printf("无此学生编号!\n");
       else 
         printf("此学生成绩:\n%d\t%s\t%f\n",s[y].id,s[y].name,s[y].score);

}