用结构体数组来实现随机查找
定义student结构体数组,下标和学号对应,即可实现学号直接查找学生
因为学号前面带0,所以要用aoti函数转string为int
#include<string>
using namespace std;
struct student{
string num;
string name;
string gender;
int age;
bool flag;
student():flag(false){}
student(string n, string na, string g,int a):flag(true),num(n),name(na),gender(g),age(a){}
};
int main(){
struct student stus[1000];
int n;
cin>>n;
for(int i=0;i<n;i++){
string num, name, gender;
int age;
cin>>num>>name>>gender>>age;
stus[atoi(num.c_str())] = student(num,name,gender,age);
}
int m;
cin>>m;
for(int i=0;i<m;i++){
int x;
cin>>x;
if(stus[x].flag)
cout<<stus[x].num<<" "<<stus[x].name<<" "<<stus[x].gender<<" "<<stus[x].age<<endl;
else
cout<<"No Answer!"<<endl;
}
}

京公网安备 11010502036488号