1036 Boys vs Girls(38行代码)

分数 25

全屏浏览题目

切换布局

作者 CHEN, Yue

单位 浙江大学

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.

Input Specification:

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 namegenderID 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.

Output Specification:

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.

Sample Input 1:

3
Joe M Math990112 89
Mike M CS991301 100
Mary F EE990830 95

Sample Output 1:

Mary EE990830
Joe Math990112
6

Sample Input 2:

1
Jean M AA980920 60

Sample Output 2:

Absent
Jean AA980920
NA

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

#include<bits/stdc++.h>
using namespace std;
const int N=109;
int n;
string namegirl,idgirl,nameboy,idboy;
int gradegirl=-1,gradeboy=101;
bool existboy=false,existgirl=false;
int main(){
    cin>>n;
    for(int i=0;i<n;i++){
        string name,sex,id;
        int grade;
        cin>>name>>sex>>id>>grade;
        if(sex=="M"){
            if(grade<gradeboy){
                nameboy=name;
                idboy=id;
                gradeboy=grade;
                existboy=true;
            }
        }
        else{
            if(grade>gradegirl){
                namegirl=name;
                idgirl=id;
                gradegirl=grade;
                existgirl=true;
            }
        }
    }
    if(existgirl)cout<<namegirl<<' '<<idgirl<<endl;
    else cout<<"Absent"<<endl;
    if(existboy)cout<<nameboy<<' '<<idboy<<endl;
    else cout<<"Absent"<<endl;
    if(existgirl&&existboy)cout<<abs(gradeboy-gradegirl)<<endl;
    else cout<<"NA"<<endl;
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
06-02
下面是一个Python实现的代码示例: ```python from queue import Queue # 定义男生队列和女生队列 boys = Queue() girls = Queue() # 输入男女生数量 m = int(input("请输入女生数量:")) n = int(input("请输入男生数量:")) # 初始化男生和女生队列 for i in range(1, m + 1): girls.put(i) for i in range(1, n + 1): boys.put(i) # 记录配对次数 count = 0 while not boys.empty() and not girls.empty(): count += 1 boy = boys.get() girl = girls.get() print("第%d曲:%d号男生和%d号女生配对跳舞" % (count, boy, girl)) # 如果配对失败,则将男生和女生重新放回各自的队列中 if count % 2 == 0: boys.put(boy) girls.put(girl) # 输入需要查询的男生和女生编号 x = int(input("请输入男生编号X:")) y = int(input("请输入女生编号Y:")) # 在男生队列中查找X的位置 boys_copy = Queue() index_x = -1 while not boys.empty(): boy = boys.get() index_x += 1 if boy == x: break boys_copy.put(boy) while not boys_copy.empty(): boy = boys_copy.get() boys.put(boy) # 在女生队列中查找Y的位置 girls_copy = Queue() index_y = -1 while not girls.empty(): girl = girls.get() index_y += 1 if girl == y: break girls_copy.put(girl) while not girls_copy.empty(): girl = girls_copy.get() girls.put(girl) # 计算X和Y在第几曲配对跳舞 if index_x == -1 or index_y == -1: print("无法配对") else: k1 = index_x + index_y + 1 k2 = k1 + n if k1 % 2 == 0 else k1 + m print("%d号男生和%d号女生在第%d曲和第%d曲配对跳舞" % (x, y, k1, k2)) ``` 在以上代码中,我们使用了Python内置的队列数据结构来实现男生队列和女生队列,并使用while循环和条件判断来模拟舞会配对的过程。在第二个问题中,我们先使用两个while循环分别在男生队列和女生队列中查找X和Y的位置,然后根据上述算法计算出X和Y在第几曲配对跳舞。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值