PAT Grade A-Finding Element Type-1036 Boys vs Girls Problem Solving Ideas

1036 Boys vs Girls (25 分)

Insert picture description here

Ideas

To find the maximum and minimum values, string use string is still very advantageous.

Code

#include <bits/stdc++.h>
using namespace std;

int main()
{
    
    
    int n;

    scanf("%d",&n);

    string name,id;
    char gender;
    string name1 = "Absent",name2="Absent",id1,id2;
    int score,score1=0,score2=10000;

    for(int i =0;i<n;i++)
    {
    
    
        cin>>name>>gender>>id>>score;
        if (gender =='F')
        {
    
    
            if(score>score1)
            {
    
    
                name1=name;
                id1 = id;
                score1 = score;
            }
        }
         if (gender =='M')
        {
    
    
            if(score<score2)
            {
    
    
                name2=name;
                id2 = id;
                score2 = score;
            }
        }
    }
    int abs_score =abs(score1-score2);

    int flag =0;
    if(name1 == "Absent")
    {
    
    
       flag = 1;
        cout<<name1<<endl;
    }
    else
        cout<<name1<<' '<<id1<<endl;

    if(name2 == "Absent")
    {
    
    
        flag =1;
        cout<<name2<<endl;
    }
    else
        cout<<name2<<' '<<id2<<endl;

    if (flag)
        cout<<"NA";
    else
        cout<<abs_score;
    

Guess you like

Origin blog.csdn.net/weixin_43999137/article/details/114017710