洛谷 P2010 回文日期

来源:互联网 发布:一洋淘宝工具箱 编辑:程序博客网 时间:2024/06/05 05:12

这里写图片描述

1.从起始日期到终止日期枚举2.由于只有8位,可以用长整型3.回文判断:    每一次转化成字符串s     或者    直接利用div,mod计算首尾位判断4.考虑day的情况   28的时候如果是闰年且是二月就       year+0,month+1,day=0   29的时候如果是平年且是二月就       year+0,month+1,day=0   30的时候如果是month in [4,6,9,11] 就        year+0,month+1,day=0   31的时候      day in [28,29,30] 没有清零      所以 month in [1,3,5,7,8,10,12]      这时候month有两种情况:      1.month=12 :year+1,month=1,day=0      2.month<>12: year+0,month+1,day=0   5.day+1   6.把year,month,day转化成八位数;     更新first。
var   ans,first,last,year,month,day,i:longint;   s:string;   flag:boolean;begin     assign(input,'date.in'); reset(input);     assign(output,'date.out');rewrite(output);    readln(first);    readln(last);    ans:=0;    while first<=last do          begin              str(first,s);              if (s[1]=s[8]) and (s[2]=s[7]) and (S[3]=s[6]) and (s[4]=s[5])                    then flag:=true                 else flag:=false;              if flag then inc(ans);              year:=first div 10000;              month:=first mod 10000 div 100;              day:=first mod 100;              case day of                   28:if (year mod 4<>0) and (month=2) then                         begin                             month:=month+1;                             day:=0;                         end;                   29:begin                         if (((year mod 4=0) and (year mod 100<>0))                         or (year mod 400=0)) and (month=2) then                         begin                             month:=month+1;                             day:=0;                         end;                      end;                   30:if (month=4) or  (month=6) or (month=9) or (month=11)                      then begin                              month:=month+1;                              day:=0;                           end;                   31:if month=12 then                         begin                             year:=year+1;                             month:=1;                             day:=0;                         end                         else begin                                  month:=month+1;                                  day:=0;                              end;              end;              day:=day+1;                   first:=year*10000+month*100+day;          end;    writeln(ans);    close(input); close(output);end.
2 0
原创粉丝点击