B1028 人口普查(已解决)

绝了!这道题最大的收获就是!用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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

moumoumouwang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值