1028. 人口普查

题目在这里


用字符串存日期,然后逐个判断,比最大日期(2014/09/06)小的是最年长的人,比最小日期(1814/09/06)大的是最年轻的人,然后将名字存下来,判断下来一次覆盖名字和日期,最后留下最年长的和最年轻的。

注:我提交时有一个点没过去,显示的问题是格式不对,后来检查了count是0的情况,发现输出为0时后面多了一个空格,所以才加上了对于count是否为0的判断。


#include <iostream>
using namespace std;

int main()
{
	int n;
	cin >> n;
	string max = "1814/09/06";
	string min = "2014/09/06";
	string maxname = min, minname = max;
	string n1, n2;
	int count = 0;
	for(int i = 0; i < n; i++) {
		string s, day;
		cin >> s >> day;
		if(day >= max && day <= min) {
			count++;
			if(day <= maxname) {
				n1 = s;
				maxname = day;
			}
			if(day >= minname){
				minname = day;
				n2 = s;
			}
		}
	}
	if(count == 0) {
		cout << count;
	} else {
		cout << count << " " << n1 << " " << n2;	
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_39227338/article/details/80245847