ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

PAT Basic Level 1028 人口普查(C++实现)

2021-09-22 15:00:40  阅读:205  来源: 互联网

标签:PAT Level young 1028 month persons year old day


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

这里确保每个输入的日期都是合法的,但不一定是合理的——假设已知镇上没有超过 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

思路

统计0-200岁的人数
设置变量存储最年长的和最年轻的

注意一定不要给最年长的和最年轻的赋初值(初值可能不合理的)!!
没有合理的日期,只输出0

AC代码


#include<iostream>

using namespace std;

struct Person
{
	char name[6];
	int year;
	int month;
	int day;
};

int main()
{
	int n;
	scanf("%d", &n);
	Person *persons = new Person[n];
	char name[6];
	int year, month, day;
	int old, young;// 注意一定不要给最年长的和最年轻的赋初值
	int count = 0;
	for (int i = 0; i < n; i++)
	{
		scanf("%s %d/%d/%d", persons[i].name, &year, &month, &day);
		if (year > 2014 || year < 1814)
			continue;
		if (year == 2014)
		{
			if (month > 9)
				continue;
			else if(month == 9)
			{
				if (day > 6)
					continue;
			}
		}
		if (year == 1814)
		{
			if (month < 9)
				continue;
			else if (month == 9)
				if (day < 6)
					continue;
		}

		if (count > 0)
		{
			if (persons[old].year > year)
				old = i;
			else if (persons[old].year == year)
			{
				if (persons[old].month > month)
					old = i;
				else if (persons[old].month == month)
					if (persons[old].day > day)
						old = i;
			}
			if (persons[young].year < year)
				young = i;
			else if (persons[young].year == year)
			{
				if (persons[young].month < month)
					young = i;
				else if (persons[young].month == month)
					if (persons[young].day < day)
						young = i;
			}
		}
		
		persons[i].year = year;
		persons[i].month = month;
		persons[i].day = day;
		if (!count)
		{
			old = i;
			young = i;
		}
		count++;
	}
	if (count == 0)
		printf("0");
	else
		printf("%d %s %s", count, persons[old].name, persons[young].name);
	return 0;
}

标签:PAT,Level,young,1028,month,persons,year,old,day
来源: https://blog.csdn.net/qq_41035097/article/details/120415003

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有