# 26 条题解

• @ 2018-11-02 11:13:10

有人说枚举会超时
可是我就是枚举做的

``````#include<bits/stdc++.h>
using namespace std;
int bc[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
bool check(int x)
{
int tmp=x,n=0;
while(tmp>=10)
{
n=n*10+tmp%10;
tmp/=10;
}
n=n*10+tmp;
//cout<<n<<" "<<x<<":\n";
if(n==x) return true;
return false;
}
int main()
{
int a,b,cnt=0;scanf("%d%d",&a,&b);
for(int i=a;i<=b;i++)
{
bool flag=0;//是不是闰年
int day=i%100;
int mon=(i/100)%100;
int year=i/10000;
if(year%4==0)
{
if(year%100==0)
{
if(year%400==0)
flag=1;
}
else flag=1;
}
if(flag)
{
if(mon==2)
{
if(day>29) continue;
}
else if(day > bc[mon]) continue;
}
else if(day > bc[mon]) continue;
if(mon > 12) continue;
if(check(i))
{
//printf("%d\n",i);
cnt++;
}
}
printf("%d",cnt);
return 0;
}
``````
• @ 2022-01-12 18:02:47

啊确实我就是枚举做的

var
yy,mm,dd,sum,yydl,mmdl,dddl,l,i,dx,k:longint;
s1,s2,sl,yz,mz,dz:string;
zw,hw:array[1..8]of char;
ding,ohh:boolean;
begin
val(s1[1..4],yy,l);val(s1[5..6],mm,l);val(s1[7..8],dd,l);
val(s2[1..4],yydl,l);val(s2[5..6],mmdl,l);val(s2[7..8],dddl,l);
ding:=false;
while ding=false do
begin
str(yy,yz);
for i:=1 to 4 do zw[i]:=yz[i];
if(mm<10)then
begin
zw[5]:='0';
str(mm,sl);
zw[6]:=sl[1];
end else begin
str(mm,mz);
for i:=5 to 6 do zw[i]:=mz[i-4]
end;
if(dd<10)then
begin
zw[7]:='0';
str(dd,sl);
zw[8]:=sl[1];
end else begin
str(dd,dz);
for i:=7 to 8 do zw[i]:=dz[i-6];
end;
for i:=1 to 8 do hw[i]:=zw[9-i];
ohh:=true;k:=0;
repeat
inc(k);
if zw[k]<>hw[k]then ohh:=false;
until(ohh=false)or(k=8);
if(k=8)and(ohh=true)then inc(sum);
if(yy=yydl)and(mm=mmdl)and(dd=dddl)then ding:=true
else begin
if(mm=1)or(mm=3)or(mm=5)or(mm=8)or(mm=7)or(mm=10)or(mm=12)then dx:=31
else if(mm=4)or(mm=6)or(mm=9)or(mm=11)then dx:=30
else begin
if(yy mod 4=0)and(yy mod 100<>0)or(yy mod 400=0)then dx:=29
else dx:=28;
end;
inc(dd);
if dd>dx then
begin
dd:=1;
inc(mm);
end;
if mm>12then
begin
mm:=1;
inc(yy);
end;
end;
end;
end.

• @ 2021-08-30 09:04:37
``````#include <bits/stdc++.h>
using namespace std;

int main()
{
int date1,date2,year1,year2,fan_year,aim_date,tmp_year,month,day,cnt=0;
int days[13] = {0,31,29,31,30,31,30,31,31,30,31,30,31};
cin>>date1>>date2;
year1=date1/10000;
year2=date2/10000;
for(int year=year1; year<=year2; year++){
tmp_year=year;
fan_year=0;
for(int i=1; i<=4; i++){
fan_year=tmp_year%10+fan_year*10;
tmp_year/=10;
}
month=fan_year/100;
day=fan_year%100;
aim_date=year*10000+month*100+day;
if(month<=12 && month>=1 && day<=days[month] && aim_date>=date1 && aim_date<=date2)
cnt++;
}
cout<<cnt;
return 0;
}
``````
• @ 2017-10-07 18:45:08

这题其实不用枚举，时间消耗比较多。。（分秒必争呀）
其实先把年份枚举，再反推末四位，看符不符合。
注意2月29日的回文日期为9220年02月29日，特判一下就可以了，其他的2月只要超过28就return false
感觉数据还是水了点，其实我的代码有些漏洞呀。。等于首尾年份是应该再看看是否在那个范围里（还好数据水）
代码：

``````#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
bool dao(int f)
{
int aa=f%10;
f=f/10;
int bb=f%10;
f=f/10;
int cc=f%10,dd=f/10;
int ab=aa*10+bb,dc=cc*10+dd;
if(ab==2&&dc==29) return true;
if(ab>12||ab<1) return false;
if((dc>31)||((ab==4||ab==6||ab==9||ab==11)&&dc==31)||(ab==2&&dc>=29)) return false;
return true;
}
int main()
{
int a,b,c,d,e=0;
cin>>a>>b;
c=a/10000;
d=b/10000;
for(int i=c;i<=d;++i)
{
if(dao(i)==true) e++;
}
cout<<e;
return 0;
}
``````
• @ 2017-09-17 10:52:21

寀鸡

• @ 2017-09-17 10:52:55

大哥威武！！！

• @ 2018-09-02 19:10:53

hhhhh

``````
``````
• @ 2020-04-13 15:16:36
``````/*
根本不需要枚举， 注意每年中最多有一天是回文就行
*/
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

bool Bigyear(int year)
{
if(year % 4 != 0 || (year % 100 == 0 && year % 400 != 0))
return false;
return true;
}

bool Date(string date)
{
int year, month, day;
year = stoi(date.substr(0, 4));
month = stoi(date.substr(4, 2));
day = stoi(date.substr(6, 2));
if(month <= 0 || month >= 13 || day <= 0 || day >= 32)
return false;
if(month == 4 || month == 6 || month == 9 || month == 11)
{
if(day == 31)
return false;
}
else if(month == 2)
{
if(Bigyear(year))
{
if(day >= 30)
return false;
}
else
if(day >= 29)
return false;
}
return true;
}

int judge(string date1, string date2, string date)
{
if(stoi(date) >= stoi(date1) && stoi(date) <= stoi(date2) && Date(date))
return 1;
return 0;
}

int Ans(string date1, string date2)
{
int ans = 0;
string s1, s2;
int k = stoi(date1.substr(0, 4));
int M = stoi(date2.substr(0, 4));
while (k <= M)
{
s2 = s1 = to_string(k);
reverse(s2.begin(), s2.end());
s1 += s2;
ans += judge(date1, date2, s1);
k++;
}
return ans;
}

int main()
{
string date1, date2;
cin >> date1 >> date2;

cout << Ans(date1, date2) << endl;

return 0;
}

``````
• @ 2018-01-23 01:29:14

吐槽一下Vijos的C编译器居然不认识gets()&&itoa();
May the father of understanding guide U;

• @ 2017-02-15 13:33:06
``````#include <cstdio>
int m[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
inline bool check(int date) {
int t[9];
t[0] = 0;
while (date) {
t[++t[0]] = date%10;
date /= 10;
}
for (int i = 1;i <= 4;i++)
if (t[i] != t[8-i+1]) return false;
return true;
}
inline int next(int i) {
int year,month,day;
day = (i%10)+((i/10%10)*10);
i /= 100;
month = (i%10)+((i/10%10)*10);
i /= 100;
year = (i%10)+((i/10%10)*10)+((i/100%10)*100)+i/1000*1000;
if ((!(year%4) && year%100) || !(year%400)) m[2] = 29;
day++;
if (day == m[month]+1) {
day = 1;
month++;
}
if (month == 13) {
month = 1;
year++;
}
return day+month*100+year*10000;
}
int main() {
//freopen("date.in","r",stdin);
//freopen("date.out","w",stdout);
int date1,date2,ans = 0;
scanf("%d%d",&date1,&date2);
for (int i = date1;i <= date2;i = next(i))
if (check(i)) ans++;
printf("%d",ans);
return 0;
}
``````
• @ 2017-02-15 09:15:56

编译成功

测试数据 #0: Accepted, time = 0 ms, mem = 732 KiB, score = 10
测试数据 #1: Accepted, time = 15 ms, mem = 728 KiB, score = 10
测试数据 #2: Accepted, time = 0 ms, mem = 732 KiB, score = 10
测试数据 #3: Accepted, time = 0 ms, mem = 732 KiB, score = 10
测试数据 #4: Accepted, time = 0 ms, mem = 732 KiB, score = 10
测试数据 #5: Accepted, time = 0 ms, mem = 732 KiB, score = 10
测试数据 #6: Accepted, time = 0 ms, mem = 732 KiB, score = 10
测试数据 #7: Accepted, time = 15 ms, mem = 728 KiB, score = 10
测试数据 #8: Accepted, time = 93 ms, mem = 732 KiB, score = 10
测试数据 #9: Accepted, time = 156 ms, mem = 736 KiB, score = 10
Accepted, time = 279 ms, mem = 736 KiB, score = 100

这题太缩水了。。。

• @ 2017-02-15 09:07:08

大家想一想，这道题千万不能用枚举，不要相信任何枚举内容

• @ 2017-02-15 09:07:26

枚举肯定会超时

• @ 2017-02-15 09:09:26

枚举可以做啊。。。

• @ 2017-02-15 09:06:54
``````#include <bits/stdc++.h>
using namespace std;

bool hw(int a)
{
int x[11];
for(int i=0;i<8;a/=10,i++) {
x[i]=a%10;
}
for(int i=0,j=7;i<4;i++,j--) {
if(x[i]!=x[j]) return 0;
}
return 1;
}
int main()
{
int i,j,k,l=0,n,m;
cin>>n>>m;
int x=n;
for(;x<=m;) {
bool run=false;
if(x/10000%400==0||(x/10000%100!=0&&x/10000%4==0)) run=true;
short flag;
k=x/100%100;
if(k==1||k==3||k==5||k==7||k==8||k==10||k==12) flag=2;
else if(k==2) {
if(run) flag=0;
else {
flag=-1;
}
}
else flag=1;
if(flag==2&&x%100==32) x+=69;
else if(flag==1&&x%100==31) x+=70;
else if(flag==0&&x%100==30) x+=71;
else if(flag==-1&&x%100==29) x+=72;
k=x/100%100;
if(k==13) x=x+8800;
if(hw(x)) l++;
x++;
}
cout<<l<<endl;
return 0;
}
``````
• @ 2017-02-15 09:07:31

。。。

• @ 2017-02-15 09:07:41

枚举都能AC

• @ 2017-02-15 09:16:11

你的题解有问题

• @ 2017-02-15 09:06:42
``````#include <bits/stdc++.h>
using namespace std;

bool hw(int a)
{
int x[11];
for(int i=0;i<8;a/=10,i++) {
x[i]=a%10;
}
for(int i=0,j=7;i<4;i++,j--) {
if(x[i]!=x[j]) return 0;
}
return 1;
}
int main()
{
int i,j,k,l=0,n,m;
cin>>n>>m;
int x=n;
for(;x<=m;) {
bool run=false;
if(x/10000%400==0||(x/10000%100!=0&&x/10000%4==0)) run=true;
short flag;
k=x/100%100;
if(k==1||k==3||k==5||k==7||k==8||k==10||k==12) flag=2;
else if(k==2) {
if(run) flag=0;
else {
flag=-1;
}
}
else flag=1;
if(flag==2&&x%100==32) x+=69;
else if(flag==1&&x%100==31) x+=70;
else if(flag==0&&x%100==30) x+=71;
else if(flag==-1&&x%100==29) x+=72;
k=x/100%100;
if(k==13) x=x+8800;
if(hw(x)) l++;
x++;
}
cout<<l<<endl;
return 0;
}
```c++
``````
• @ 2017-08-25 00:24:51

so water

``````#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
int mon[13]={-1,31,28,31,30,31,30,31,31,30,31,30,31};
int main()
{
int d1,d2,f=0;
scanf("%d\n%d",&d1,&d2);
int y1=d1/10000;
int y2=d2/10000;
int sum=0;
for(int i=y1;i<=y2;i++)
{
int d=i*10000+i%10*1000+i%100/10*100+i%1000/100*10+i%10000/1000;
int m=d%10000/100;
int day=d%100;
if(m>=1&&m<=12)
{
if(day>=1&&day<=mon[m])
sum++;
}
if(i==9220)
sum++;
}
printf("%d",sum);
return 0;
}
``````
• @ 2017-09-17 10:44:27

操你吗傻逼

• @ 2017-09-17 10:47:08

渣渣*

• @ 2017-07-27 23:02:05

超长代码
const c:array[1..12] of integer=(31,29,31,30,31,30,31,31,30,31,30,31);
var n,i,s,k:longint;
a,b:array[1..3] of integer;
s1,s2:string;
procedure re;
var i,sx:integer;
begin
sx:=1000;
for i:=1 to 8 do
begin
if i<5 then begin
a[1]:=a[1]+(ord(s1[i])-48)*sx;
sx:=sx div 10;
end;
if (i<7)and(i>4) then begin
a[2]:=a[2]+(ord(s1[i])-48)*sx;
sx:=sx div 10
end;
if (i<9)and(i>6) then begin
a[3]:=a[3]+(ord(s1[i])-48)*sx;
sx:=sx div 10;
end;
if (i=4)or(i=6) then sx:=10;
end;
end;
procedure rea;
var i,sx:integer;
begin
sx:=1000;
for i:=1 to 8 do
begin
if i<5 then begin
b[1]:=b[1]+(ord(s2[i])-48)*sx;
sx:=sx div 10;
end;
if (i<7)and(i>4) then begin
b[2]:=b[2]+(ord(s2[i])-48)*sx;
sx:=sx div 10;
end;
if (i<9)and(i>6) then begin
b[3]:=b[3]+(ord(s2[i])-48)*sx;
sx:=sx div 10;
end;
if (i=4)or(i=6) then sx:=10;
end;
end;
procedure pp(x:integer);
begin
if ((x mod 4=0)and(x mod 100<>0))or(x mod 400=0) then c[2]:=29
else c[2]:=28;
end;
function check(y,z:string):boolean;
begin
if (y[1]=z[4])and(y[2]=z[3])and(y[3]=z[2])and(y[4]=z[1]) then exit(true)
else exit(false);
end;
begin
if s1=s2 then
begin
s2:=copy(s1,5,4);
delete(s1,5,4);
if check(s1,s2) then writeln(1)
else writeln(0);
halt;
end;
fillchar(a,sizeof(a),0);re;
fillchar(b,sizeof(b),0);rea;
s:=0;
pp(a[1]);
while (a[1]<>b[1])or(a[2]<>b[2])or(a[3]<>b[2]) do
begin
k:=a[2]*100+a[3];
s1:='';s2:='';str(a[1],s1);str(k,s2);
if a[2]<10 then insert('0',s2,1);
if check(s1,s2) then inc(s);
inc(a[3]);
if a[3]>c[a[2]] then
begin
a[3]:=1;
inc(a[2]);
end;
if a[2]>12 then
begin
a[2]:=1;
inc(a[1]);
pp(a[1]);
end;
end;
writeln(s);
end.

• @ 2017-07-25 13:34:28

完全不用枚举
根据年份构造回文串判断是否合法即可
强悍的O（1）
```cpp #include<bits/stdc++.h> using namespace std; int mon[13]={-1,31,28,31,30,31,30,31,31,30,31,30,31}; int main() { int d1,d2,f=0; cin>>d1>>d2; int y1=d1/10000; int y2=d2/10000; int sum=0; for(int i=y1;i<=y2;i++) { int d=i*10000+i%10*1000+i%100/10*100+i%1000/100*10+i%10000/1000; int m=d%10000/100; int day=d%100; //cout<<d<<" "<<i<<"/"<<m<<"/"<<day<<endl; if(m>=1&&m<=12) { if(day>=1&&day<=mon[m])sum++;//cout<<d<<" "<<i<<"/"<<m<<"/"<<day<<endl; } if(i==9220)sum++; } cout<<sum<<endl; return 0; } ```

• @ 2017-07-15 11:05:42

帮我看看错哪了
var
s1,s2:string;
s,i,j,y1,y2,m,d,x,y:longint;
f:byte;
begin
val(copy(s1,1,4),y1);val(copy(s2,1,4),y2);
for i:=y1 to y2 do begin
f:=0;
m:=i mod 10*10+i mod 100 div 10;
d:=i div 100 mod 10*10+i div 1000;
if (m>=1)and(m<=12) then f:=1;
if (f=1) then begin
if (m=1)or(m=3)or(m=5)or(m=7)or(m=8)or(m=10)or(m=12) then begin
if (d>=1)and(d<=31) then f:=2;
end
else begin
if (m<>2) then if (d>=1)and(d<=30) then f:=2
else begin
if (i mod 4=0)and(i mod 100<>0)or(i mod 400=0) then begin
if (d>=1)and(d<=29) then f:=2;
end
else if (d>=1)and(d<=28) then f:=2;
end;
end;
end;
if i=y1 then begin
val(copy(s1,5,2),x);val(copy(s1,7,2),y);
if (f=2)and(m>=x)and(d>=y)then inc(s);
end;
if (i=y2)and(i<>y1) then begin
val(copy(s2,5,2),x);val(copy(s2,7,2),y);
if (f=2)and(m<=x)and(d<=y)then inc(s);
end;
if (i>y1)and(i<y2)then if f=2 then inc(s);
end;
writeln(s);
end.

• @ 2017-07-15 11:04:06

var
s1,s2:string;
s,i,j,y1,y2,m,d,x,y:longint;
f:byte;
begin
val(copy(s1,1,4),y1);val(copy(s2,1,4),y2);
for i:=y1 to y2 do begin
f:=0;
m:=i mod 10*10+i mod 100 div 10;
d:=i div 100 mod 10*10+i div 1000;
if (m>=1)and(m<=12) then f:=1;
if (f=1) then begin
if (m=1)or(m=3)or(m=5)or(m=7)or(m=8)or(m=10)or(m=12) then begin
if (d>=1)and(d<=31) then f:=2;
end
else begin
if (m<>2) then if (d>=1)and(d<=30) then f:=2
else begin
if (i mod 4=0)and(i mod 100<>0)or(i mod 400=0) then begin
if (d>=1)and(d<=29) then f:=2;
end
else if (d>=1)and(d<=28) then f:=2;
end;
end;
end;
if i=y1 then begin
val(copy(s1,5,2),x);val(copy(s1,7,2),y);
if (f=2)and(m>=x)and(d>=y)then inc(s);
end;
if (i=y2)and(i<>y1) then begin
val(copy(s2,5,2),x);val(copy(s2,7,2),y);
if (f=2)and(m<=x)and(d<=y)then inc(s);
end;
if (i>y1)and(i<y2)then if f=2 then inc(s);
end;
writeln(s);
end.

• @ 2017-03-22 14:26:15

主要考判断语句，从开始年份到目标年份枚举，每年月份枚举，注意月份的天数有的31有的30，还要判断是不是为闰年or平年，如此便穷举出所有有效日期，随后判断计数输出即可。
```pascal
var
d1,d2,sy,ey,i,j,k,s,ans:longint;

function pan(s:longint):boolean;
var
i:longint;
st:string;
begin
str(s,st);
for i:= 1 to 4 do
begin
if st[i]<>st[8-i+1] then exit(false);
end;
exit(true);
end;
begin
sy:=d1 div 10000;
ey:=d2 div 10000;
for i:= sy to ey do
begin
for j:= 1 to 12 do
begin
if (j=1) or (j=3) or(j=5) or (j=7) or (j=8) or (j=10) or (j=12) then
begin
for k:= 1 to 31 do
begin
s:=i*10000+j*100+k;
if (s>=d1) and (s<=d2) then
if pan(s) then inc(ans);
end;
end
else if (j=2) then
begin
if ( (i mod 4 =0) and (i mod 100 <>0 )) or (i mod 400=0) then
begin
for k:= 1 to 29 do
begin
s:=i*10000+j*100+k;
if (s>=d1) and (s<=d2) then
if pan(s) then inc(ans);
end;
end
else
begin
for k:= 1 to 28 do
begin
s:=i*10000+j*100+k;
if (s>=d1) and (s<=d2) then
if pan(s) then inc(ans);
end;
end;
end
else
begin
for k:= 1 to 30 do
begin
s:=i*10000+j*100+k;
if (s>=d1) and (s<=d2) then
if pan(s) then inc(ans);
end;
end;
end;
end;
writeln(ans);
end.
```

• @ 2017-03-22 14:26:46
``````var
d1,d2,sy,ey,i,j,k,s,ans:longint;

function pan(s:longint):boolean;
var
i:longint;
st:string;
begin
str(s,st);
for i:= 1 to 4 do
begin
if st[i]<>st[8-i+1] then exit(false);
end;
exit(true);
end;
begin
sy:=d1 div 10000;
ey:=d2 div 10000;
for i:= sy to ey do
begin
for j:= 1 to 12 do
begin
if (j=1) or (j=3) or(j=5) or (j=7) or (j=8) or (j=10) or (j=12) then
begin
for k:= 1 to 31 do
begin
s:=i*10000+j*100+k;
if (s>=d1) and (s<=d2) then
if pan(s) then inc(ans);
end;
end
else if (j=2) then
begin
if ( (i mod 4 =0) and (i mod 100 <>0 )) or (i mod 400=0) then
begin
for k:= 1 to 29 do
begin
s:=i*10000+j*100+k;
if (s>=d1) and (s<=d2) then
if pan(s) then inc(ans);
end;
end
else
begin
for k:= 1 to 28 do
begin
s:=i*10000+j*100+k;
if (s>=d1) and (s<=d2) then
if pan(s) then inc(ans);
end;
end;
end
else
begin
for k:= 1 to 30 do
begin
s:=i*10000+j*100+k;
if (s>=d1) and (s<=d2) then
if pan(s) then inc(ans);
end;
end;
end;
end;
writeln(ans);
end.
``````
• @ 2017-07-18 17:16:14

强悍的枚举
居然没有超时
我记得我考试的时候用枚举结果一个个tle

• @ 2018-09-02 19:12:40

@xieshihua: NA

• @ 2017-02-18 20:27:49

#include <iostream>
#include<algorithm>
using namespace std;
int Sovle(int a[],int x,int n,int R)
{
int i=x,j;
while(a[i]-R<a[x]&&i<n)
{
i++;
}
if(i==n&&a[i]-R<a[x])
return 1;
i--;
for(j=i;j<n;j++)
if(a[j]>a[i]+R)
break;
return 1+Sovle(a,j,n,R);
}
int main ()
{
int R,x[10000],N;
cin>>R>>N;
for(int i=0;i<N;i++)
cin>>x[i];
sort(x,x+N);
cout<<Sovle(x,0,N,R);
return 0;
}

• @ 2017-02-18 17:30:13

#include <iostream>
#include <string>
using namespace std;
int get(string s,bool x)
{
if(x==true)
return (s[0]-48)*1000+(s[1]-48)*100+(s[2]-48)*10+(s[3]-48);
else
return (s[7]-48)*1000+(s[6]-48)*100+(s[5]-48)*10+(s[4]-48);
}
void change(int x,int a[])
{
int i,xx=x;
for(i=0;i<4;i++)
a[i]=xx%10,xx/=10;
}
bool judge(int a[],int year)
{
int month=a[0]*10+a[1],day=a[2]*10+a[3];
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
{
if(day<=31)
{
return true;
}
}
if(month==4||month==6||month==9||month==11)
{
if(day<=30)
{
return true;
}
}
if(month==2)
{
if(year%4==0&&year%100!=0||year%400==0)
{
if(day<=29)
{
return true;
}
}
else
{
if(day<=28)
{
return true;
}
}
}
return false;
}
int main ()
{
string start,end;
cin>>start>>end;
int s=get(start,true),e=get(end,true),a[4],sum=0;
if(get(start,true)<get(start,false))
s++;
if(get(end,true)<get(end,false))
e--;
while(s<=e)
{
change(s,a);
if(judge(a,s)==true)
sum++;
s++;
}
cout<<sum;
return 0;
}

ID
2010

4

(无)

1320

327

25%

21