对日期的处理与判断
闰年判断方式1.能被四整除但是不能被100整除
2. 能被400整除
#include<iostream> #include<algorithm> #include<cstring> using namespace std ; char s[10] ; int months[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31}; bool check(int year,int month,int day){ if(month==0||month>=13) return false ; if(day==0) return false ; if(month!=2){ if(day > months[month]) return false ; }else { bool leap = (year%4==0 && year%100) || (year%400==0) ; if(day > 28 + leap) return false ; } return true ; } int main(){ for(int i = 0 ; i < 8; i ++) cin >> s[i] ; int a = (s[0] - '0')*10 + (s[1] - '0') ; int b = (s[3] - '0')*10 + (s[4] - '0') ; int c = (s[6] - '0')*10 + (s[7] - '0' ); for(int i = 19600101; i <= 20591231 ; i++){ int year = i/10000 , month = i%10000/100 ,day = i%100 ; if(check(year,month,day)){ if((year%100 == a&&month == b && day == c)||(year%100==c&&month==a&&day==b)||(year%100==c&&month==b&&day==a)){ printf("%d-%02d-%02d\n",year,month,day) ; } } } }