Boys vs Girls (25)

本文介绍了一道编程题,任务是找出一组学生中男生最低分数与女生最高分数的差距,并展示了解决方案的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Boys vs Girls (25)

时间限制 1000 ms 内存限制 65536 KB 代码长度限制 100 KB 判断程序 Standard (来自 小小)
题目描述
This time you are asked to tell the difference between the lowest grade of all the male students and the highest grade of all the female students.

输入描述:
Each input file contains one test case. Each case contains a positive integer N, followed by N lines of student information. Each line contains a student’s name, gender, ID and grade, separated by a space, where name and ID are strings of no more than 10 characters with no space, gender is either F (female) or M (male), and grade is an integer between 0 and 100. It is guaranteed that all the grades are distinct.

输出描述:
For each test case, output in 3 lines. The first line gives the name and ID of the female student with the highest grade, and the second line gives that of the male student with the lowest grade. The third line gives the difference gradeF-gradeM. If one such kind of student is missing, output “Absent” in the corresponding line, and output “NA” in the third line instead.

输入例子:
3
Joe M Math990112 89
Mike M CS991301 100
Mary F EE990830 95

输出例子:
Mary EE990830
Joe Math990112
6

题解:字符串和最值的查找,不断更新temp和temp1,分别找到男最低值和女最高值相减,注意题意条件判断。

#include <bits/stdc++.h>
#include <stdlib.h>
using namespace std;
#define vi vector<int>
#define pii pair<int,int>
#define x first
#define y second
#define all(x) x.begin(),x.end()
#define pb push_back
#define mp make_pair
#define SZ(x) x.size()
#define rep(i,a,b) for(int i=a;i<b;i++)
#define per(i,a,b) for(int i=b-1;i>=a;i--)
#define pi acos(-1)
#define mod 1000000007
#define inf 1000000007
#define ll long long
#define DBG(x) cerr<<(#x)<<"="<<x<<"\n";
#define N 200010
template <class U,class T> void Max(U &x, T y){if(x<y)x=y;}
template <class U,class T> void Min(U &x, T y){if(x>y)x=y;}
template <class T> void add(int &a,T b){a=(a+b)%mod;}
const int maxn=100001;
struct stu
{
    string name;
    char gen;
    string id;
    int sc;
}st[maxn];
int main()
{
    int n,f1=0,f2=0;
    cin>>n;
    rep(i,0,n)
    {
        cin>>st[i].name>>st[i].gen>>st[i].id>>st[i].sc;
    }
    stu temp,temp2;
    temp.sc=100,temp2.sc=0;
    rep(i,0,n)
    {
        if((st[i].sc<temp.sc)&&(st[i].gen=='M'))
        {
            temp=st[i];
            f1=1;
        }
        if((st[i].sc>temp2.sc)&&(st[i].gen=='F'))
        {
            temp2=st[i];
            f2=1;
        }
    }
    if(f2==1)
    {
        cout<<temp2.name<<" "<<temp2.id<<endl;
    }
    else
    {
        cout<<"Absent"<<endl;
    }
    if(f1==1)
    {
        cout<<temp.name<<" "<<temp.id<<endl;
    }
    else
    {
        cout<<"Absent"<<endl;
    }
    if(f1==1&&f2==1)
    {
        cout<<temp2.sc-temp.sc<<endl;
    }
    else
    {
        cout<<"NA"<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值