【PTA刷题整理】PAT 乙级 1028 人口普查

2020.03.24 今天把题目早上给写了,上的项目实践基础提前下课了,就看了一眼题目感觉挺简单的,就马上动手了


1028 人口普查 (20分)

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

这里确保每个输入的日期都是合法的,但不一定是合理的——假设已知镇上没有超过 200 岁的老人,而今天是 2014 年 9 月 6 日,所以超过 200 岁的生日和未出生的生日都是不合理的,应该被过滤掉。
输入格式:

输入在第一行给出正整数 N,取值在(0,10​5​​];随后 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


首先是输入的存储,直接使用结构体,然后关于含“/”的输入,就用scanf规避掉就好了,不用再做进一步的分割,因为时间限制是200ms,所以简单粗暴的使用if-else进行处理就好了,但是绝对不是最好的办法,再定义一个计数器,将有效的生日数据放入vector容器,自定义cmp函数,进行sort排序,最后进行输出即可
当然,这个题目你也可以直接把1814.09.06-2014.09.06的天数范围算出来,这样会耗费的时间少很多


#include<iostream>                  //输入输出流头文件
#include<stdio.h>                   //标准输入输出
#include<stdlib.h>
#include<math.h>                    //数学函数
#include<string.h>                  //C语言字符数组的字符串
#include<algorithm>                 //C++标准模板库的函数
#include<map>                       //map映射容器
#include<unordered_map>             //无序的map映射容器
#include<vector>                    //变长数组容器
#include<queue>                     //队列
#include<stack>                     //栈
#include<string>                    //C++string类
#include<set>                       //set集合
using namespace std;                //标准命名空间

                                    //可以加入全局变量或者其他函数

struct Person{
	char name[8];
	int yyyy;
	int mm;
	int dd;
};

bool cmp(Person a , Person b){
	if(a.yyyy > b.yyyy){
		return true;
	}else if(a.yyyy < b.yyyy){
		return false;
	}else{
		if(a.mm > b.mm){
			return true;
		}else if(a.mm < b.mm){
			return false;
		}else{
			if(a.dd > b.dd){
				return true;
			}else{
				return false;
			}
		}
	}
}

int main(){                         //主函数
#ifdef ONLINE_JUDGE                 //如果有oj系统(在线判定),则忽略文件读入,否则使用文件作为标准输入
#else
    freopen("1.txt", "r", stdin);   //从1.txt输入数据
#endif
	int N;
	cin >> N;
	int counter = 0;
	vector<Person> ans;
	Person temp;
	for(int i = 0 ; i < N ; i++){
		scanf("%s %d/%d/%d", temp.name , &temp.yyyy , &temp.mm , &temp.dd );
		if(temp.yyyy > 2014 || temp.yyyy < 1814){
			continue;
		} 
		if((temp.yyyy == 2014 && temp.mm > 9) || 
		(temp.yyyy == 1814 && temp.mm < 9)){
			continue;
		} 
		if((temp.yyyy == 2014 && temp.mm == 9 && temp.dd > 6) || 
		(temp.yyyy == 1814 && temp.mm == 9 && temp.dd < 6)){
			continue;
		}
		ans.push_back(temp);
		counter++;
	} 
	sort(ans.rbegin() , ans.rend() , cmp);
//	for(auto iter = ans.begin() ; iter != ans.end() ; iter++){
//		cout << iter->name << iter->yyyy << iter->mm << iter->dd << endl;
//	}
	if(counter != 0){
		cout << counter  << " " <<  ans[0].name << " " << ans[counter - 1].name << endl;
	}else{
		cout << 0 << endl;
	}
	
    return 0;                       
}

花费的时间的确是太多辽,还要去其他的博客看看有没有更加简便的方法
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值