zoukankan      html  css  js  c++  java
  • PAT 乙级 1028 人口普查(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

    方法一:

     1 #include<iostream>
     2 #include<string>
     3 #include<cstdlib>
     4 #include<map>
     5 
     6 using namespace std;
     7 
     8 int main()
     9 {
    10     int N, i, num = 0,low=18140905,high=20140907;
    11     int year,month,day,value;
    12     char c;
    13     string name;
    14     map<int, string> m;
    15 
    16     cin >> N;
    17 
    18     for (i = 0; i < N; ++i)
    19     {
    20         cin.get();
    21         cin >> name >> year >> c >> month >> c >> day;
    22         
    23         value=year*10000+month*100+day;
    24 
    25         if (value > low&&value < high)
    26         {
    27             m.insert(pair<int, string>(value, name));
    28 
    29             ++num;
    30         }
    31     }
    32 
    33     map<int, string>::iterator max = m.begin();
    34     map<int, string>::reverse_iterator min = m.rbegin();
    35 
    36     if (num > 0)
    37         cout << num << " " << max->second << " " << min->second << endl;
    38     else
    39         cout << 0 << endl;
    40 
    41     return 0;
    42 }

    方法二:用生日类比较

     1 #include<iostream>
     2 #include<string>
     3 #include<cstdlib>
     4 #include<map>
     5 
     6 using namespace std;
     7 
     8 class birthday
     9 {
    10 public:
    11     int year;
    12     int month;
    13     int day;
    14 public:
    15     bool operator>(const birthday& bir) const;
    16     bool operator<(const birthday& bir) const;
    17 };
    18 
    19 int main()
    20 {
    21     int N, i, num = 0;
    22     char c;
    23     string name;
    24     birthday b, low = { 1814, 9, 5 }, high = { 2014, 9, 7 };
    25     map<birthday, string> m;
    26 
    27     cin >> N;
    28 
    29     for (i = 0; i < N; ++i)
    30     {
    31         cin.get();
    32         cin >> name >> b.year >> c >> b.month >> c >> b.day;
    33 
    34         if (b > low&&b < high)
    35         {
    36             m.insert(pair<birthday, string>(b, name));
    37 
    38             ++num;
    39         }
    40     }
    41 
    42     map<birthday, string>::iterator max = m.begin();
    43     map<birthday, string>::reverse_iterator min = m.rbegin();
    44 
    45     if (num > 0)
    46         cout << num << " " << max->second << " " << min->second << endl;
    47     else
    48         cout << 0 << endl;
    49 
    50     return 0;
    51 }
    52 
    53 //比较出生日期,出生日期大(晚出生)返回1
    54 bool birthday::operator>(const birthday& bir) const
    55 {
    56     if (this->year > bir.year)
    57         return 1;
    58     else if (this->year == bir.year&&this->month > bir.month)
    59         return 1;
    60     else if (this->year == bir.year&&this->month == bir.month&&this->day > bir.day)
    61         return 1;
    62     else
    63         return 0;
    64 }
    65 
    66 //比较出生日期,出生日期小(早出生)返回1
    67 bool birthday::operator<(const birthday& bir) const
    68 {
    69     if (this->year < bir.year)
    70         return 1;
    71     else if (this->year == bir.year&&this->month < bir.month)
    72         return 1;
    73     else if (this->year == bir.year&&this->month == bir.month&&this->day < bir.day)
    74         return 1;
    75     else
    76         return 0;
    77 }
  • 相关阅读:
    菜单项向子页面传递参数
    Grid中添加链接,打开选项卡页面
    FineUI与百度地图简单示例 (转帖)
    AppBox中main树节点单击事件JS(还有叶子的节点的页面链接)
    FineUI中在一个页面中通过控件事件(JS)向父页面中添加Tab页
    如何使用button在tab中新建打开一个链接页
    系统service
    官员详解官场对领导称谓讲究:叫大不叫小
    搞笑对话
    陆琪:男人生存的意义,就是赚钱养老婆
  • 原文地址:https://www.cnblogs.com/cdp1591652208/p/8185759.html
Copyright © 2011-2022 走看看