蓝桥杯 回文日期 emmm

原题链接:蓝桥杯 回文日期
emmmm这道题,就是有那么一点点坑。。。
首先要判断数字是否满足日期格式(原来闰年2月才是29天,非闰年2月是28天,我给记反了。。。)
然后肯定很多人看到对于所有评测用例,10000101≤N≤89991231,就设了日期不能超过89991231,反而,人家说的是输入日期。。。。 代码一直过不了,实在不解,把while(n1<=89991231)改成while(1)就行了。。害

#include <bits/stdc++.h>
using namespace std;

bool fun1(string s)
{
    for(int i=0;i<4;i++)
    {
        if(s[i]!=s[7-i])
            return false;
    }
    return true;
}
int a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};

bool fun2(string s)
{
    string s1=s.substr(0,2);
    string s2=s.substr(2,2);
    string s3=s.substr(4,2);
    string s4=s.substr(6,2);
    reverse(s3.begin(),s3.end());
    reverse(s4.begin(),s4.end());
    if(s1==s2 && s2==s3 && s3==s4)
        return true;
    else
        return false;
}
bool isrun(int y)
{
    if((y%4==0 && y%100!=0) || (y%400==0))
        return true;
    return false;
}
int main()
{
   std::ios::sync_with_stdio(false);
   string str;
   int n;
   cin>>n;
   int f1=0,f2=0;
   int n1=n;
   int year,month,day;
   while(1)
   {
       year=n1/10000;
       month=n1/100%100;
       day=n1%100;
       if(month==12 && day==31)
       {
           year++; month=1; day=1;
       }
       else if(isrun(year) && month==2)
       {
           if(day==29)
           {
               month=3; day=1;
           }
           else
             day++;
       }
       else if(!(isrun(year)) || (isrun(year) && month!=2) )
       {
           if(day==a[month])
           {
               month++; day=1;
           }
           else
             day++;
       }
       if(month>=1 && month<=9)
       {
           n1=year*10000+(10+month)*100+day-1000;
       }
       else
       {
           n1=year*10000+month*100+day;
       }
       stringstream ss;
       ss<<n1;
       string s=ss.str();
       if(fun1(s) && f1==0)
       {
           f1=1;
           cout<<n1<<endl;
       }
       if(fun1(s) && fun2(s))
       {
           f2=1;
           cout<<n1<<endl;
       }
       if(f1==1 && f2==1)
       {
           break;
       }
   }
   return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值