人口普查(20)

题目描述

某城镇进行人口普查,得到了全体居民的生日。现请你写个程序,找出镇上最年长和最年轻的人。

这里确保每个输入的日期都是合法的,但不一定是合理的——假设已知镇上没有超过200岁的老人,而今天是2014年9月6日,所以超过200

岁的生日和未出生的生日都是不合理的,应该被过滤掉。

输入描述:

输入在第一行给出正整数N,取值在(0, 105];随后N行,每行给出1个人的姓名(由不超过5个英文字母组成的字符>串)、以及

按“yyyy/mm/dd”(即年/月/日)格式给出的生日。题目保证最年长和最年轻的人没有并列。

输出描述:

在一行中顺序输出有效生日的个数、最年长人和最年轻人的姓名,其间以空格分隔。

输入例子:

5

John 2001/05/12

Tom 1814/09/06

Ann 2121/01/30

James 1814/09/05

Steve 1967/11/20

输出例子:

3 Tom John

思路:
我自己写的是自定义一个结构,像这样:

struct People 
{ 
    char name[20]; 
    int year; 
    int month; 
    int day; 
} people[MAX]; 

虽然运行通过了,但是代码较长,因为比较日期太麻烦了,后来看到网友用stl里的sort算法比较日期大小,直接用string存储年月日,并用sort函数比较,效率很高。在这里学习一下。

代码(来自牛客网网友:夭夭):

#include 
#include 
using namespace std;
struct Person
{
    string name;
    string birthday;
};
bool compare(const Person& s1, const Person& s2)//参数为指针所以要用&
{
    if (s1.birthday < s2.birthday)//升序,若改为s1.birthday > s2.birthday,则为降序
        return true;
    else
        return false;
}
int main()
{
    int N;
    vector man;
    Person temp;
    string compare1 = "2014/09/06";
    string compare2 = "1814/09/06";
    cin >> N;
    for ( int i = 0; i < N; i++)
    {
        cin >> temp.name >> temp.birthday;
        if (temp.birthday < compare1 && temp.birthday >= compare2)
            man.push_back(temp);
    }
    sort(man.begin(), man.end(), compare); //man.begin()/end()为指针,front(),back()为数据
    cout <" " <" "<return 0;
}

关于sort的详细用法看这里

vector的相关知识和函数看这里

附上我的代码做个比较:

#include  
#include  
#include  
#include  
using namespace std; 

#define MAX 100000+10 
struct People 
{ 
    char name[20]; 
    int year; 
    int month; 
    int day; 
} people[MAX]; 

int isLegal(People p) 
{ 
    if (p.year > 1814 && p.year < 2014) 
        return 1; 
    else if (p.year == 1814) 
    { 
        if (p.month > 9) 
            return 1; 
        else if (p.month == 9 && p.day >= 6) 
            return 1; 
    } 
    else if (p.year == 2014) 
    { 
        if (p.month < 9) 
            return 1; 
        else if (p.month == 9 && p.day <= 6) 
            return 1; 
    } 
    return 0; 
} 

int younger(People p1, People p2) 
{ 
    if (p1.year > p2.year ) 
        return 1; 
    else if (p1.year == p2.year && p1.month > p2.month) 
        return 1; 
    else if (p1.year == p2.year && p1.month == p2.month && p1.day > p2.day) 
        return 1; 
    return 0; 
} 

int older(People p1, People p2) 
{ 
    if (p1.year < p2.year ) 
        return 1; 
    else if (p1.year == p2.year && p1.month < p2.month) 
        return 1; 
    else if (p1.year == p2.year && p1.month == p2.month && p1.day < p2.day) 
        return 1; 
    return 0; 
} 


int main() 
{ 
    //freopen("in.txt", "r", stdin); 
    //freopen("out.txt", "w", stdout); 
    int n = 0; 
    cin >> n; 
    for (int i = 0; i < n; i++) 
    { 
        scanf("%s", people[i].name); 
        scanf("%d/%d/%d", &people[i].year, &people[i].month, &people[i].day); 
    } 

    //qsort(people, n, sizeof(people[0]), cmp); 

    int cnt = 0; 
    People youngpeople= {"", 0, 0, 0}, oldpeople= {"", 9999, 9999, 9999}; 
    for (int i = 0; i < n; i++) 
    { 
        if (isLegal(people[i])) 
        { 
            if (older(people[i], oldpeople)) 
                oldpeople = people[i]; 
            if (younger(people[i], youngpeople)) 
                youngpeople = people[i]; 
            cnt++; 
        } 
    } 

    cout << cnt << " " << oldpeople.name << " " << youngpeople.name; 
    return 0; 
}

你可能感兴趣的:(PAT)