一开始有考虑可能给出的日期在要求的范围内,但可能是违法的,比如2010/02/30 这种,但是先做简单是试探——只按照题目给出的无效情况来写编程。 利用string容器的operator和erase 可以将每个人的出生时间转换成这样的格式——20100230 ,接着用这个字符串来和范围比较,确定是否有效。
*并且要考虑输出时若有效的个数为零,则之后不输出名字,也就不需要空格了,这也是一个点。*
#include <cstdio>
#include <vector>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;
struct people{
string nianling;
string name;
}pp,oldest,youngest;
int main(){
int cnt = 0; //记录总数
int n,i,z=0;
string::iterator it;
scanf("%d",&n);
for(i=0; i<n; i++){
cin>>pp.name>>pp.nianling; //cin 和 scanf我都试了下>>'/'>>%d ,确认不能和输出做类比。
for(it=pp.nianling.begin(); it!=pp.nianling.end(); it++){
if(*it=='/') pp.nianling.erase(it,it+1);
}
if(pp.nianling<="20140906" && pp.nianling>="18140906"){
cnt++;z++;
if(z==1) {
oldest = pp;
youngest = pp;
}else{
oldest=oldest.nianling>pp.nianling?oldest:pp;
youngest=youngest.nianling<pp.nianling?youngest:pp;
}
}
}
if(cnt==0)
{
printf("0");
}else cout << cnt << " " << youngest.name <<" "<<oldest.name;
return 0;
}
还请大家多多指出代码冗余之处以及题目的新思路。
转载请注明出处。