顺序查找
今天开始每天做一道算法或者数据结构相关的练习题!今天从最简单的开始顺序查找,其实就是遍历咯!
#include "stdio.h" typedef struct student{ int id; char name[10]; float score; }Student; int search(Student stu[],int n,int key){ int i; for(i=0;i<n;i++){ if(stu[i].id == key) return i; } return -1; } int main() { Student stu[4] = { {1004,"TOM",100}, {1002,"LILY",95}, {1001,"ANN",93}, {1003,"LUCY",98} }; int addr; addr = search(stu,4,1001); printf("Student ID: %dn",stu[addr].id); printf("Student name: %sn",stu[addr].name); printf("Student score: %fn",stu[addr].score); }
这个用到的基本算法思想是:穷举法