一、题目
二、代码
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int n;
cin >> n;
int reallyn = n;
string oldname, youngname;
string min = "2014/09/06", max = "1814/09/06";
string name, birthday;
for (int i = 0; i < n; i++) {
cin >> name >> birthday;
if (birthday > "2014/09/06" || birthday < "1814/09/06")
{ reallyn--; }
else {
if (birthday >= max) {
max = birthday;
youngname = name;
}
if (birthday <= min) {
min = birthday;
oldname = name;
}
}
}
cout << reallyn;
//判断有无有效信息
if (reallyn != 0) {
cout<<" "<< oldname << " " << youngname;
}
}
三、分析
1.读题分析:
不合理:需要筛选超过200和未出生的(2014年9月6日),记录有效个数
名字:长度在五个之内
结果无并列
测试结果可知,范围在1814.09.06-2014.09.06
2.输出
最年长的人:year最小的人,最年轻的人:year最大的人
3.get
string可以比较!!!!之前一直想着不能比较,就开始拆分生日,搞得特别复杂。
思路搞对这道题真的简简单单,搞错超级复杂(可参考下方错误代码
4.碎碎念念
想复杂了又,刚开始的思路为先装到vector中然后在进行循环判断,判断条件写了一堆,特别复杂,然后看网上,发现在cin时就能直接判断,因为本题不需要全部的数据输入,只需要最后的max和min两个数据就好!!!!!
5.错误代码(真的好直观)柳神!!!
#include <iostream>
#include <algorithm>
#include<vector>
#include<string>
using namespace std;
struct people {
string name;
string yy, mm, dd;
};
int main() {
int n;
cin >> n;
int reallyn = n;
vector<people>v(n);
for (int i = 0; i < n; i++) {
cin >> v[i].name;
string ymd;
cin >> ymd;
//拆分,进入vector
int num = 0;
for (int j = 0; j < ymd.size(); j++) {
if (ymd[j] == '/') {
num++;
continue;
}
if (num == 0) {
v[i].yy = v[i].yy + ymd[j];
}
else if (num == 1) {
v[i].mm = v[i].mm + ymd[j];
}
else if (num == 2) {
v[i].dd = v[i].dd + ymd[j];
}
}
if (v[i].yy >= "2014" && v[i].mm >= "09" && v[i].dd >= "06") {
reallyn--;
}
if (v[i].yy <= "1814" && v[i].mm <= "09" && v[i].dd <= "06") {
reallyn--;
}
}
//判断最大最小;
string maxname, minname;
string maxyy = "1814", maxmm = "09", maxdd = "06";
string minyy = "2014", minmm = "09", mindd = "06";
for (int i = 0; i < n; i++) {
if (v[i].yy>"2014"||v[i].yy < "1814"){continue;}
else if((v[i].yy=="2014"&&v[i].mm > "09")||(v[i].mm < "09"&&v[i].yy=="1814")) {continue;}
else if((v[i].yy=="2014"&&v[i].mm=="09"&&v[i].dd>"06")||(v[i].yy=="1814"&&v[i].mm=="09"&&v[i].dd<"06")){ continue;}
//判断最大
if (v[i].yy > maxyy) {
maxyy = v[i].yy, maxmm = v[i].mm, maxdd = v[i].dd;
maxname = v[i].name;
}
else if (v[i].yy == maxyy && v[i].mm > maxmm) {
maxyy = v[i].yy, maxmm = v[i].mm, maxdd = v[i].dd;
maxname = v[i].name;
}
else if (v[i].mm == maxmm && v[i].dd > maxdd) {
maxyy = v[i].yy, maxmm = v[i].mm, maxdd = v[i].dd;
maxname = v[i].name;
}
//判断最小
if (v[i].yy < minyy) {
minyy = v[i].yy, minmm = v[i].mm, mindd = v[i].dd;
minname = v[i].name;
}
else if (v[i].yy == minyy && v[i].mm < minmm) {
minyy = v[i].yy, minmm = v[i].mm, mindd = v[i].dd;
minname = v[i].name;
}
else if (v[i].mm == minmm && v[i].dd < mindd) {
minyy = v[i].yy, minmm = v[i].mm, mindd = v[i].dd;
minname = v[i].name;
}
}
cout<<reallyn;
if (reallyn!=0){
cout <<" " << minname << " " << maxname;
}
}