zoukankan      html  css  js  c++  java
  • 1036 Boys vs Girls (25 分)

    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


    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=1010;
    struct node{
        string name;
        char gender;
        string id;
        int grade;
    };
    vector<node> v;
    int main(){
        int n;
        cin>>n;
        v.resize(n);
        int maxF=-1,maxdex=-1;
        int minM=1000000000,mindex=-1;
        for(int i=0;i<n;i++){
            cin>>v[i].name>>v[i].gender>>v[i].id>>v[i].grade;
            if(v[i].gender=='F'&&v[i].grade>maxF){
                maxF=v[i].grade;
                maxdex=i;
            }
            if(v[i].gender=='M'&&v[i].grade<minM){
                minM=v[i].grade;
                mindex=i;
            }
        }
        int cnt;
        
        if(maxdex!=-1&&mindex!=-1){
            cnt=abs(v[maxdex].grade-v[mindex].grade);
            cout<<v[maxdex].name<<" "<<v[maxdex].id<<endl;
            cout<<v[mindex].name<<" "<<v[mindex].id<<endl;
            cout<<cnt<<endl;
        }
        else{
            if(maxdex==-1){
                cout<<"Absent"<<endl;
            }
            else{
                cout<<v[maxdex].name<<" "<<v[maxdex].id<<endl;
            }
            if(mindex==-1){
                cout<<"Absent"<<endl;
            }
            else{
                cout<<v[mindex].name<<" "<<v[mindex].id<<endl;
            }
            if(maxdex==-1||mindex==-1){
                cout<<"NA"<<endl;
            }
        }
        return 0;
    }

     优化:

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=1010;
    struct node{
        string name;
        char gender;
        string id;
        int grade;
    };
    vector<node> v;
    int main(){
        int n;
        cin>>n;
        v.resize(n);
        int maxF=-1,maxdex=-1;
        int minM=1000000000,mindex=-1;
        for(int i=0;i<n;i++){
            cin>>v[i].name>>v[i].gender>>v[i].id>>v[i].grade;
            if(v[i].gender=='F'&&v[i].grade>maxF){
                maxF=v[i].grade;
                maxdex=i;
            }
            if(v[i].gender=='M'&&v[i].grade<minM){
                minM=v[i].grade;
                mindex=i;
            }
        }
        if(maxdex!=-1){
            cout<<v[maxdex].name<<" "<<v[maxdex].id<<endl;
        }
        else{
            cout<<"Absent"<<endl;
        }
        if(mindex!=-1){
            cout<<v[mindex].name<<" "<<v[mindex].id<<endl;
        }
        else{
            cout<<"Absent"<<endl;
        }
        if(maxdex!=-1&&mindex!=-1){
            int cnt=abs(v[maxdex].grade-v[mindex].grade);
            cout<<cnt<<endl;
        }
        else{
            cout<<"NA"<<endl;
        }
        return 0;
    }
  • 相关阅读:
    树莓派也跑Docker和.NET Core
    使用iSCSI协议挂载网络磁盘,电脑瞬间扩大一个T的容量!
    Azure DevOps Server (TFS)免费吗?
    明确架构目标
    MMN实用架构过程概览
    设计恰如其分的架构
    对象的自治和行为的扩展与适配
    Message Chains与Fluent Interface
    如何减少代码的量
    《软件框架设计的艺术》书评
  • 原文地址:https://www.cnblogs.com/dreamzj/p/14461292.html
Copyright © 2011-2022 走看看