绝了!这道题最大的收获就是!用c++的字符串进行年月日比较!天呀天呀
打开新世界的大门!!!!!!(最后代码惊喜555字符串c++好绝)
错误思路:在判断条件满足以后,依次进行年月日的比较,以最年老为例,初始化oldyear=2014,oldmonth=9,oldday=6;如果输入结构体temp.year<=oldyear仅进行月的判断temp.month<=oldmonth接下来如果temp.day<oldday;那么更新oldyear,oldmonth,oldday,
结构体 oldest也进行更新 oldest=temp; 但是运行不出来 ,过几天再看看8.
更新正确思路
哈哈哈哈哈 我好傻啊 我居然把月日跟年一起限制了 他可以一是2013 11月11啊 月真没必要<9 日也是真没必要<6 真是没必要 害
更改后(还要注意 如果输出全部不符合的话那就输出0)
#include <iostream>
#include <cstdio>
using namespace std;
typedef struct
{
char name[20];
int year,month,day;
} Peple;
bool Right(Peple a) //右边界条件
{
if(a.year<2014)
{
return true;
}
else if(a.year == 2014)
{
if(a.month < 9)
return true;
else if(a.month == 9)
{
if(a.day <= 6)
return true;
else
return false;
}
else
{
return false;
}
}
else
{
return false;
}
}
bool Left(Peple a) //左边界条件
{
if(a.year>1814)
{
return true;
}
else if(a.year == 1814)
{
if(a.month > 9)
return true;
else if(a.month == 9)
{
if(a.day >= 6)
return true;
else
return false;
}
else
{
return false;
}
}
else
{
return false;
}
}
bool judge (Peple a,Peple b)
{
if(a.year>b.year)
{
return true;
}
else if(a.year == b.year)
{
if(a.month > b.month)
{
return true;
}
else if(a.month == b.month)
{
if(a.day >= b.day)
{
return true;
}
else
{
return false;
}
}
else
return false;
}
else
{
return false;
}
}
int main()
{
Peple yongest,oldest,temp; //最年轻最老,临时结构体储存
yongest.year = 1814;
yongest.month = 9;
yongest.day = 6;
oldest.year = 2014;
oldest.month = 9;
oldest.day = 6;
int n;
int j=0;int sum=0;
scanf("%d",&n);
//int yongyear=2014,yongmonth=9,yongday=6; //初始化 用于判断
for(int j=0; j<n; j++)
{
scanf("%s %d/%d/%d",temp.name,&temp.year,&temp.month,&temp.day);
if(Left(temp)&&Right(temp)) //如果满足条件
{
sum++;
if(judge(oldest,temp))
{
oldest = temp;
}
if(judge(temp,yongest))
{
yongest = temp;
}
}
}
if(sum==0) printf("0\n"); //如果都不合法
else
{
printf("%d %s %s\n",sum,oldest.name,yongest.name);
}
return 0;
}
第一次 错误的
#include <iostream>
#include <cstdio>
struct Person
{
char name[20];
int year,month,day;
}yongest,oldest,temp;
bool Right(Person a) //右边界条件
{
return ((a.year<=2014)&&(a.month<=9)&&(a.day<=6));
}
bool Left(Person a) //左边界条件
{
return ( (a.year>=1814)&&(a.month>=9)&&(a.day>=6));
}
int main()
{
//最年轻最老,临时结构体储存
int n;
int j=0;
int sum=0;
scanf("%d",&n);
int oldyear=2014,oldmonth=9,oldday=6; //初始化 用于判断
int yongyear=1814,yongmonth=9,yongday=6;
for(int j=0; j<n; j++)
{
scanf("%s %d/%d/%d",temp.name,&temp.year,&temp.month,&temp.day);
if(Left(temp)&&Right(temp)) //如果满足条件
{
sum++;
if(temp.year<=oldyear) //找最老的!
{
if(temp.month<=oldmonth)
{
if(temp.day<oldday)
{
oldest=temp;
oldyear=temp.year;
oldmonth=temp.month;
oldday=temp.day;
}
}
}
if(temp.year>=yongyear)
{
if(temp.month>=yongmonth)
{
if(temp.day>yongday)
{
yongest=temp;
yongyear=temp.year;
yongmonth=temp.month;
yongday=temp.day;
}
}
}
}
}
if(sum==0) printf("0\n"); //如果都不合法
else
{
printf("%d %s %s\n",sum,oldest.name,yongest.name);
}
return 0;
}
优化
C语言优化《算法笔记》大神的代码
优化思路:在输入时进行日期是否有效的判断,如果在就经过判断更新最年长的人和最年轻的人的出生日期。由于判断日期是否在合法日期区间内、更新最年长最年轻的信息都涉及比较操作,就有了LessEqu与MoreEqu这两个比较函数。
#include <cstdio>
struct Person
{
char name[20];
int year,month,day;
}yongest,oldest,temp,left,right;
//yongest,oldest,存放最年长与最年幼 left,right存放左边界右边界
bool LessEqu(Person a,Person b) //如果a的日期<=b 返回true
{
if(a.year!=b.year) return a.year<=b.year;
else if(a.month!=b.month) return a.month<=b.month;
else return a.day<=b.day;
}
bool MoreEqu(Person a,Person b) //如果a的日期>=b 返回true
{
if(a.year!=b.year) return a.year>=b.year;
else if(a.month!=b.month) return a.month>=b.month;
else return a.day>=b.day;
}
void init()
{
yongest.year=left.year=1814;
oldest.year=right.year=2014;
yongest.month=left.month=9;
oldest.month=right.month=9;
yongest.day=left.day=6;
oldest.day=right.day=6;
}
int main()
{
init(); //初始化
int n,sum=0;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%s %d/%d/%d",temp.name,&temp.year,&temp.month,&temp.day);
if(LessEqu(temp,right)&&MoreEqu(temp,left))
{
sum++;
if(LessEqu(temp,oldest)) oldest=temp;
if(MoreEqu(temp,yongest)) yongest=temp;
}
}
if(sum==0) printf("0\n"); //如果都不合法
else
{
printf("%d %s %s\n",sum,oldest.name,yongest.name);
}
return 0;
}
c++优化 又是参考的柳婼大神的代码 过于简洁简洁到令人兴奋 omg!!字符串好绝!
#include <iostream>
using namespace std;
int main()
{
string name,birth,minbirth="2014/09/06",maxbirth="1814/09/06";
string minname,maxname;//最年幼的人的名字 最年长的人的名字
int n,num=0;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>name>>birth;
if(birth>="1814/09/06"&&birth<="2014/09/06")
{
num++;
if(birth>maxbirth)
{
maxbirth=birth;
minname=name;
}
if(birth<minbirth)
{
minbirth=birth;
maxname=name;
}
}
}
cout<<num<<" "<<maxname<<" "<<minname<<endl;
return 0;
}