题目:找出8位日期(YYYYMMDD)表示法,xxxx年-xxxx年之间回文日期数目
思路:从a年枚举到b年,三重循环,从a年-b年,1-12月,对应月份每天的天数,再对每一天的进行8为日期取数(从左到右:t%10;t=t/10;),判断是否为回文数字。
注意:三重循环时i,j,k不要修改,利用变量代替!!!
#include <iostream> #include <stdio.h> using namespace std; int a,b; int c[10]; int main() { cin>>a>>b;//a年到b年,a,b>999且<10000,即为1000-9999 2024 int ans=0; int d[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; int d1[13]={0,31,29,31,30,31,30,31,31,30,31,30,31}; //从a年1月1日到b年12月31日一共回文日期数 for(int i=a;i<=b;i++) { for(int j=1;j<=12;j++) { if(i%4==0&&i%100!=0||i%400==0)//闰年 { for(int k=1;k<=d1[j];k++) { //判断是否为回文年 int t1,t2,t3;t1=i;t2=j;t3=k; for(int l=4;l>=1;l--)//取年4位 { c[l]=t1%10;t1=t1/10; } for(int l=6;l>=5;l--)//取月2位 { c[l]=t2%10;t2=t2/10; } for(int l=8;l>=7;l--)//取日2位 { c[l]=t3%10;t3=t3/10; } int flag=0; for(int l=1;l<=4;l++)//回文判断 { if(c[l]!=c[9-l]) { flag=1; } } if(flag==0) { for(int l=1;l<=8;l++) { cout<<c[l]; } cout<<endl; ans++; } } } else { for(int k=1;k<=d[j];k++) { //判断是否为回文年 int t1,t2,t3;t1=i;t2=j;t3=k; for(int l=4;l>=1;l--)//取年4位 { c[l]=t1%10;t1=t1/10; } for(int l=6;l>=5;l--)//取月2位 { c[l]=t2%10;t2=t2/10; } for(int l=8;l>=7;l--)//取日2位 { c[l]=t3%10;t3=t3/10; } int flag=0; for(int l=1;l<=4;l++)//回文判断 { if(c[l]!=c[9-l]) { flag=1; } } if(flag==0) { for(int l=1;l<=8;l++) { cout<<c[l]; } cout<<endl; ans++; } } } } } cout<<ans<<endl; return 0; }
535

被折叠的 条评论
为什么被折叠?



