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;
    }
  • 相关阅读:
    2. 商城项目完整购物链路 lq
    如何看源码? lq
    事务的了解 lq
    1. 商城业务架构分析 lq
    并发的基础知识 lq
    mysql 索引 lq
    mysqlinnodb了解 lq
    IE6.0、IE7.0 与FireFox CSS兼容的解决方法
    CSS:html/css教程:背景图片的定位问题详解
    IE6 BUG
  • 原文地址:https://www.cnblogs.com/dreamzj/p/14461292.html
Copyright © 2011-2022 走看看