1036 Boys vs Girls

xiaoxiao2025-10-19  5

题目大意:

输出女生最高分的姓名和学号和男生最低分姓名和学号,最后一行输出两者之间的差值,如果对应位置没人(没有女生或者男生),输出“Absent”,差值输出为“NA”。

解题思路:

结构体排序,代码如下:

#include<iostream> #include<cstdio> #include<fstream> #include<set> #include<cmath> #include<cstring> #include<string> #include<map> #include<vector> #include<iomanip> #include<cstdlib> #include<list> #include<queue> #include<stack> #include<algorithm> #define inf 0x3f3f3f3f #define MOD 1000000007 #define mem0(a) memset(a,0,sizeof(a)) #define mem1(a) memset(a,-1,sizeof(a)) #define meminf(a) memset(a,inf,sizeof(a)) //vector ::iterator it; //set<int>::iterator iter; typedef long long ll; typedef unsigned long long ull; using namespace std; struct male { string name; char gender; string id; int grade; }m[1001]; struct female { string name; char gender; string id; int grade; }f[1001]; int cmp2(male a,male b) { return a.grade<b.grade; } int cmp1(female a,female b) { return a.grade>b.grade; } int main() { std::ios::sync_with_stdio(false); cin.tie(0); int n,m1=0,f1=0; cin>>n; while(n--) { string name,id; char gender; int grade; cin>>name>>gender>>id>>grade; if(gender=='M')//男生 { m[m1].name=name; m[m1].gender=gender; m[m1].id=id; m[m1].grade=grade; m1++; } else///女生 { f[f1].name=name; f[f1].gender=gender; f[f1].id=id; f[f1].grade=grade; f1++; } } sort(m,m+m1,cmp2);sort(f,f+f1,cmp1);//排序 bool flag=true; if(f1==0) { cout<<"Absent"<<endl; flag=false; } else cout<<f[0].name<<" "<<f[0].id<<endl; if(m1==0) { cout<<"Absent"<<endl; flag=false; } else cout<<m[0].name<<" "<<m[0].id<<endl; if(flag)cout<<f[0].grade-m[0].grade<<endl; else cout<<"NA"<<endl; //freopen("test.txt","r",stdin); //freopen("output.txt","w",stdout); return 0; }
转载请注明原文地址: https://www.6miu.com/read-5038212.html

最新回复(0)