PAT Class B 1028 Population Census (Problem Solving Ideas + AC Code)

topic:

A town conducts a census and obtains the birthdays of all residents. Now please write a program to find the oldest and youngest people in the town.

This ensures that each date entered is valid, but not necessarily reasonable - assuming that there are no known people in town over 200, and today is September 6, 2014, so birthdays over 200 and future Birth and birthday are all unreasonable and should be filtered out.

Input format:

Enter a positive integer N in the first line , and the value is (0,105]; then N lines, each line gives a person's name (a string consisting of no more than 5 English letters), and press yyyy/mm/dd(ie year/ Date of birth given in format MM/DD). The title ensures that there is no tie between the oldest and youngest person.

Output format:

Print the number of valid birthdays and the names of the oldest and youngest in sequence on one line, separated by a space.

Input sample:

5
John 2001/05/12
Tom 1814/09/06
Ann 2121/01/30
James 1814/09/05
Steve 1967/11/20

Sample output:

3 Tom John

Code length limit 16 KB
time limit 200 ms
memory limit 64 MB

 

problem solving ideas

First, customize a Person class whose member variables include name, year, month, and day. And the = operator and > operator are overloaded, so that the Person class has the ability of assignment and comparison.

The general idea of ​​the main function:

  1. Instantiate the Person object with the possible (reasonable) maximum age (1814/09/06) and minimum age (2014/09/06), and define the dynamically changing maximum and minimum age Person objects.
  2. The dynamically changing maximum age object is initialized to 2014/09/06, and the dynamically changing minimum age object is initialized to 1814/09/06, so that the dynamically changing maximum and minimum age objects can be assigned as real objects.
  3. Use a for loop to detect a case in each round. If the age of the case is reasonable, then the number of valid birthdays will be increased by 1, and the dynamically changing maximum and minimum age objects will be updated.

Note: It is easy to overlook a situation here, that is, the number of valid birthdays may be 0, and in this case, there is no need to output the names of the maximum and minimum ages.

 

AC code

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

class Person{
    
    
public:
    Person();
    ~Person(){
    
    };
    Person(string name,int y,int m,int d)
    :_name(name),_year(y),_month(m),_day(d){
    
    }
    Person& operator=(const Person& per){
    
    
        _name=per._name;
        _year=per._year;
        _month=per._month;
        _day=per._day;
        return *this;
    }
    bool operator>(Person& per){
    
    
        if(this->_year!=per._year) return this->_year<=per._year;
        else if(this->_month!=per._month) return this->_month<=per._month;
        else return this->_day<=per._day;
    }
    string _name;
    int _year,_month,_day;
};

int main()
{
    
    
    int num=0;
    Person per1("qwe_per1",1814,9,6);
    Person per2("qwe_per2",2014,9,6);
    Person per_old=per2,per_young=per1;

    int N;
    cin>>N;
    for(int i=0;i<N;i++)
    {
    
    
        string name;
        int year,month,day;
        cin>>name;
        scanf("%d/%d/%d",&year,&month,&day);
        Person per_temp(name,year,month,day);
        if(per1>per_temp&&per_temp>per2)
        {
    
    
            num++;
            if(per_young>per_temp)per_young=per_temp;
            if(per_temp>per_old)per_old=per_temp;
        }
    }
    cout<<num;
    if (num != 0)
    {
    
    
        cout<<" "<<per_old._name<<" "<<per_young._name;
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/m0_70103775/article/details/130612307