zoukankan      html  css  js  c++  java
  • 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
    

    代码

    // 1028 人口普查.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
    
    #include <iostream>
    #include<cstdio>
    #pragma warning( disable : 4996)
    using namespace std;
    
    struct people {
    	int year;
    	int month;
    	int day;
    	string name;
    };
    
    bool greater_(people p1, people p2) {
    	return p1.year == p2.year ? p1.month == p2.month ? p1.day > p2.day:p1.month > p2.month:p1.year > p2.year;
    }
    
    bool isvalid(people p) {
    	//2012 年 9 月 6 日之前的都要过滤掉
    	people pmax, pmin;
    	pmax.year = 1814;
    	pmax.month = 9;
    	pmax.day = 6;
    	pmin.year = 2014;
    	pmin.month = 9;
    	pmin.day = 6;
    	if (greater_(pmax,p)) {
    		return false;
    	}
    	if (greater_(p,pmin)) {
    		return false;
    	}
    	return true;
    }
    
    int main()
    {
    	int n;
    	cin >> n;
    	//构造最年轻和最年老的人
    	people pmax, pmin;
    	pmax.year = 2014;
    	pmax.month = 9;
    	pmax.day = 6;
    	pmin.year = 1814;
    	pmin.month = 9;
    	pmin.day = 6;
    	int thisyear, thismonth, thisday;
    	string thisname;
    	int cnt = 0;
    	for (int i = 0; i < n; i++) {
    		people tmp;
    		cin >> thisname;
    		scanf("%d/%d/%d", &thisyear, &thismonth, &thisday);
    		tmp.year = thisyear;
    		tmp.month = thismonth;
    		tmp.day = thisday;
    		tmp.name = thisname;
    		if (isvalid(tmp)) {
    			cnt++;
    		}
    		if (isvalid(tmp)&&greater_(pmax,tmp)) {
    			pmax = tmp;
    		}
    		if (isvalid(tmp)&&greater_(tmp, pmin)) {
    			pmin = tmp;
    		}
    	}
    	if (cnt != 0) {
    		cout << cnt << " " << pmax.name << " " << pmin.name;
    	}
    	else {
    		cout << "0";
    	}
    	
    	return 0;
    }
    
  • 相关阅读:
    Servlet会话管理一(URL重写和表单隐藏域)
    Eclipse自动补全设置
    String类为什么设计成不可变的
    部署描述符(web.xml)和标注(annotation)
    html中 &nbsp; 和空格的区别
    HTML的基本知识点
    Eclipse中配置Tomcat服务器并创建标准Web目录
    Sevlet处理HTML表单
    Servlet API
    Servlet开发的三种方法
  • 原文地址:https://www.cnblogs.com/ericling/p/12340562.html
Copyright © 2011-2022 走看看