当前位置: 代码迷 >> 综合 >> pta--1036 Boys vs Girls(25 分)(结构体)
  详细解决方案

pta--1036 Boys vs Girls(25 分)(结构体)

热度:99   发布时间:2023-12-26 10:10:04.0

题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805453203030016

【分析】定义结构体,然后排序输出再做判断就好;

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
struct node{string name;string id;int grade;
}s1[1005],s2[1005];
int cmp(node s1,node s2)
{return s1.grade<s2.grade;
}
int main()
{int n,cnt1=0,cnt2=0;scanf("%d",&n);for(int i=0;i<n;i++){string names;char sex;cin>>names;getchar();cin>>sex;if(sex=='F'){s1[cnt1].name=names;//cout<<s1[i].name<<endl;cin>>s1[cnt1].id>>s1[cnt1].grade;cnt1++;}else if(sex=='M'){s2[cnt2].name=names;cin>>s2[cnt2].id>>s2[cnt2].grade;cnt2++;}}//cout<<s1[0].name<<endl;sort(s1,s1+cnt1,cmp);//Fsort(s2,s2+cnt2,cmp);//Mif(cnt1==0)cout<<"Absent\n"<<s2[0].name<<" "<<s2[0].id<<"\nNA\n";else if(cnt2==0)cout<<s1[n-1].name<<" "<<s1[n-1].id<<"\nAbsent\n"<<"NA\n";else cout<<s1[cnt1-1].name<<" "<<s1[cnt1-1].id<<"\n"<<s2[0].name<<" "<<s2[0].id<<"\n"<<s1[cnt1-1].grade-s2[0].grade<<endl;return 0;
}