解题思路: 按照题目意思,根据题目给出的函数完成相应的功能即可。
参考代码:
#include<iostream> #include<cstring> using namespace std; typedef struct { char name[21]; char num[11]; char sex; }Student; void readdata(Student stu[],int n) { for(int i=0;i<n;i++) cin>>stu[i].name>>stu[i].num>>stu[i].sex; } int findstudent(Student stu[],int n,char *data) { if (data==NULL) return -1; for(int i=0;i<n;i++) { if(strcmp(stu[i].name,data)==0 || strcmp(stu[i].num,data)==0) return i; } return -1; } void solve(Student stu[],int n,int m) { char x[21],y[21]; for(int i=0;i<m;i++) { cin>>x>>y; int x_k=findstudent(stu,n,x); int y_k=findstudent(stu,n,y); if(x_k!=-1 && y_k!=-1) { if(stu[x_k].sex!=stu[y_k].sex) cout<<"Y"<<endl; else cout<<"N"<<endl; } else cout<<"N"<<endl; } } int main() { int n,m; Student stu[1010]; cin>>n; readdata(stu,n); cin>>m; solve(stu,n,m); return 0; }
0.0分
8 人评分