本来要求的是c,真的太麻烦了,选c++
#include <iostream>
#include <string>
using namespace std;
struct stu{
int id;
string name;
};
void find1(int id,stu student[])
{
for(int i=0;i<sizeof(student)+1;i++)
{
if(student[i].id==id) cout << student[i].name<<endl;
}
}
void find2( int target,stu student[])//循环实现
{
int low = 0 ,high = sizeof(student) +1, middle;
while(low < high)
{
middle = (low + high)/2;
if(target == student[middle].id)
{
cout<<student[middle].name;
return;
}
else if(target > student[middle].id)
low = middle +1;
else if(target < student[middle].id)
high = middle;
}
};
int main()
{
stu student[5];
string names[]={"wang","zhang","li","liu","zhao"};
for(int i=0;i<5;i++)
{
student[i].id=i+1;
student[i].name=names[i];
}
find1(1,student);
find2(5,student);
return 0;
}