一道有价值的题

回文日期

https://ac.nowcoder.com/acm/problem/16438

原题的数据有一些漏洞 ,现在重新修改了代码。
修复了会被hack的小漏洞
如果输入是
20211111
20211111
输出应该是0
但是输出却是1 此时的1 指的是20211202,但是这个数字应该在范围外,不应该被统计在内。

//一下是原文
这是一道枚举题,在开始前老师告诉我我们可以用构造,枚举月日推年份,然后试了一下发现太复杂,我有特别懒,不想写那么长的代码,然后我就换成了已知年份推月日,简单的调了一下就ac了,看了其他题解,如果把月用数组存起来可以避免像我这么麻烦的判断。
#include <bits/stdc++.h>
using namespace std;
int runnian(int year, int month)
{
if (month == 2)
{
if (year % 4 == 0 && (!(year % 100) || year % 400))
return 29;
else
return 28;
}
switch (month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
default:
return 30;
}
}
int main()
{
int l, r;
int sum=0;
cin >> l >> r;
int year, month, date;
// int a[10], b[10]; //a left b right
int l1 = l/10000, r1 = r/10000,l2=l%10000,r2=r%10000;

for (int i=l1;i<=r1;i++)
{
    int i1=i;
    int month1= i%10*10+i%100/10;
    int data1= i%1000/100*10+i/1000;
    int  whole=month1*100+data1;
    if (i==l1&&whole<l2)
    {
        continue;
    }
    if (i==r1&&whole>r2)
    break;
    if (runnian(year,month1)>=data1&&month1<=12&&month1>=1)
    {
    sum++;
    }

}
cout << sum << endl;
system("pause");
return 0;

}

全部评论

相关推荐

1 收藏 评论
分享
牛客网
牛客企业服务