[PAT (Basic Level) Practice] - [find element] 1028

I. [Topic difficulty]

  • Class B

II. [Title No.]

  • 1028 census (20 points)

III. [Title Description]

  • A census was conducted in a town and the birthdays of all residents were obtained. Now please write a program to find out the oldest and youngest people in the town.
  • Here, ensure that each entered date is legal, but not necessarily reasonable - assuming that there are no people over 200 years old in the town, and today is September 6, 2014, so birthdays over 200 and unborn birthdays are unreasonable and should be filtered out.

IV. [Title example]

  • Input format:
    The input gives a positive integer on the first line N N N. Value in ( 0 , 1 0 5 ] (0,10^5 ] (0105]; later N N N lines, each line gives the name of one person (a string composed of no more than 5 English letters), and press y y y y / m m / d d yyyy/mm/dd The birthday given in yyyy/mm/dd (i.e. year / month / day) format. The title ensures that the oldest and youngest people are not tied.

  • Output format:
    Output the number of valid birthdays and the names of the oldest and youngest in one row, separated by spaces.

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

  • Output example:
    3 Tom John

V. [problem solving ideas]

  • The general idea of this question is 1004 score ranking (20 points) Almost all of them create a new structure to save the main information, and then get the results through comparison. However, the problem is more complex. The specific steps are as follows:
  • ① : New P e r s o n Person The Person structure stores the Person's name and birthday
  • ② : New l e s s less less function, the function is a < = b ( year , month , day ) A < = B (year, month, day) A < = B (year, month, day) return$ t r u e true true, otherwise return f a l s e false false
  • ③ : New m o r e more more function, the function is > = b ( year , month , day ) >=B (year, month, day) >=B (year, month, day) return$ t r u e true true, otherwise return f a l s e false false
  • ④ : New i n i t init init function. This function is used to initialize parameters. young is the youngest, old is the oldest, start is the minimum boundary, and end is the maximum boundary
  • ⑤ : New m a i n main Main main function, the number of people scanned is n, and the initialization num is 0. The function of this parameter is to save the legal number of data, and then start scanning and input line by line. If the input data is legal, Num + +, and if the current data (temp) is younger than the youngest, it should be updated. Note that it cannot be used here i f ... e l s e if...else If... else judgment, because the youngest and oldest may be the same person, and the output will be after scanning. Here's another point to note: if num is 0, the special judgment output will be 0, otherwise the format error will be caused by more spaces later, and the rest can be output normally

Vi. [final score]

  • 20 points

VII. [code implementation]

#include<stdio.h>
#include<stdbool.h>

struct Person
{
    char name[10];
    int y,m,d;
}old,young,start,end,temp;

bool less(struct Person a,struct Person b)
{
    if(a.y != b.y)
    {
        return a.y <= b.y;
    }
    else if(a.m != b.m)
    {
        return a.m <= b.m;
    }
    else
    {
        return a.d <= b.d;
    }
}

bool more(struct Person a,struct Person b)
{
    if(a.y != b.y)
    {
        return a.y >= b.y;
    }
    else if(a.m != b.m)
    {
        return a.m >= b.m;
    }
    else
    {
        return a.d >= b.d;
    }
}

void init()
{
    young.y = 1814;
    young.m = 9;
    young.d = 6;
    old.y = 2014;
    old.m = 9;
    old.d = 6;
    start.y = 1814;
    start.m = 9;
    start.d = 6;
    end.y = 2014;
    end.m = 9;
    end.d = 6;
}

int main()
{
    init();
    int n = 0,num = 0;
    scanf("%d",&n);
    for(int i = 0;i<n;i++)
    {
        scanf("%s %d/%d/%d",&temp.name,&temp.y,&temp.m,&temp.d);
        if(less(start,temp) && more(end,temp))
        {
            num++;
            if(more(old,temp))
            {
                old = temp;
            }
            if(less(young,temp))
            {
                young = temp;
            }
        }
    }
    if(num == 0)
    {
        printf("0\n");
    }
    else
    {
        printf("%d %s %s\n",num,old.name,young.name);
    }
    return 0;
}

VIII. [submission results]

Keywords: C Algorithm data structure Dynamic Programming Graph Theory

Added by phprocker on Sat, 08 Jan 2022 11:17:34 +0200