求大牛指点

P2010 [NOIP2016 普及组] 回文日期

Mikola @ 2016-11-26 18:55:32

var
  d1,d2,s:string;
  ans:longint;
function re(s:string):boolean;
var
  i:longint;
begin
  for i:=1 to 4 do
    if s[i]<>s[9-i]
      then
        exit(false);
  exit(true);
end;
procedure addone;
var
  st:string;
  t,p,x:longint;
begin
  s[8]:=chr(ord(s[8])+1);
  if ord(s[8])=(ord('9')+1)
    then
      begin
        s[7]:=chr(ord(s[7])+1);
        s[8]:='0';
      end;
  st:=s[7]+s[8];
  val(st,t);
  st:=s[5]+s[6];
  val(st,p);
  st:=s[1]+s[2]+s[3]+s[4];
  val(st,x);
  if (p in [1,3,5,7,8,10,12]) and (t>31)
    or(p in [4,6,9,11]) and (t>30)
    or (((x mod 4=0) and (x mod 100<>0)) or (x mod 400=0)) and (p=2) and (t>29)
    then
      begin
        s[6]:=chr(ord(s[6])+1);
        if ord(s[6])=(ord('9')+1)
          then
            begin
              s[5]:=chr(ord(s[5])+1);
              s[6]:='0';
            end;
        s[7]:='0';
        s[8]:='1';
      end
    else

if t>28

        then
          begin
            s[6]:=chr(ord(s[6])+1);
            if ord(s[6])=(ord('9')+1)
              then
                begin
                  s[5]:=chr(ord(s[5])+1);
                  s[6]:='0';
                end;
             s[7]:='0';
             s[8]:='1';
          end;
  st:=s[5]+s[6];

if st>'12'

    then
      begin
        s[5]:='0';
        s[6]:='0';
        s[4]:=chr(ord(s[4])+1);
        if ord(s[4])=(ord('9')+1)
          then
            begin
              s[3]:=chr(ord(s[3])+1);
              s[4]:='0';
            end;
        if ord(s[3])=(ord('9')+1)
          then
            begin
              s[2]:=chr(ord(s[2])+1);
              s[3]:='0';
            end;
        if ord(s[2])=(ord('9')+1)
          then
            begin
              s[1]:=chr(ord(s[1])+1);
              s[2]:='0';
            end;
      end;
end;
begin
  assign(input,'date.in');
  assign(output,'date.out');
  reset(input);
  rewrite(output);
  readln(d1);
  readln(d2);
  if d1=d2
    then
      begin
        if re(d1)
          then
            writeln(1)
          else
            writeln(0);
        close(input);
        close(output);
        halt;
     end;
  s:=d1;
  repeat
    if re(s)
      then
        inc(ans);
    addone;
    writeln(s);
  until s>d2;
  writeln(ans);
  close(input);
  close(output);
end.

by hexuan @ 2016-12-02 13:01:48

var  a,b,ans,x,y,a1,a2,a3,a4,d1,d2,d3,days:longint;
begin 
  //assign(input,'date.in');  reset(input);
  //assign(output,'date.out');  rewrite(output);
  read(a,b);
  x:=a div 10000-1;//年份
  ans:=0;
  repeat 
    inc(x);
    a1:=x mod 10;  a2:=(x div 10) mod 10;  a3:=(x div 100) mod 10;  
    a4:=x div 1000;
    y:=x*10000+a1*1000+a2*100+a3*10+a4;//回文数
    if (y>=a) and (y<=b) then//判断是否在范围内
    begin 
      d1:=(y mod 10000) div 100;
      d2:=y mod 100;  d3:=y div 10000;  
      if (d1<=12) and (d1>0) then //判断日期是否合法 
      begin    
        case d1 of     
        1,3,5,7,8,10,12: days:=31;      
        4,6,9,11: days:=30;      
        2: if ((d3 mod 100<>0) and (d3 mod 4=0)) or (d3 mod 400=0) then days:=29       
             else days:=28;
        end;
      end;     
      if (days>=d2) and (d1<=12) and (d1>0) then ans:=ans+1;   
    end; 
  until y>b; 
  write(ans);
  //close(input);  close(output);
end.

by hexuan @ 2016-12-02 13:03:19

这是数学暴力算法


by 鲁班七号 @ 2016-12-02 22:24:01

确定不TLE??


by hexuan @ 2016-12-06 13:00:57

AC,没TLE


|