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;
    }
  • 相关阅读:
    OC ----关于时间的处理
    OC-- 判断字符串是否是纯数字
    OC 判断字符串是否只存在只有空格
    将Json字符串转换为数组
    调用接口时对参数的排序、生成签名、生成随机数,获取唯一标示符
    关于UIAlertView弹出警告框自动消失
    AVAudioPlayer 获取语音的长度(时间)
    AFN 请求 https get
    TableView  — reloadData     刷新
    mapView 地图视图
  • 原文地址:https://www.cnblogs.com/dreamzj/p/14461292.html
Copyright © 2011-2022 走看看