The number of days of the statistical date, mainly considering leap year situations.
Ideas:
- Every year 1, 3, 5, 7, 8, 10, December are 31 days
- Every year 4, 6, 9, November is 30 days
- The year of February has 29 days, other years of February are 28 days
- Run year has 2 kinds of situation:
- Can be divisible by 4 but not divisible by 100 (for example, 2004 is leap year, 1800 is not)
- Can be divisible by 400 (e.g. 2000 is a leap year)
Topic 1: Enter a date and the output is the day ordinal of the current year.
1#include <iostream>2#include <stdio.h>3 using namespacestd;4 5 intIsrun (intYear )6 {7 if(((year%4==0) && (year% -!=0))|| year% -==0)return 1;8 Else return 0;9 }Ten intDaysum (intYearintMonthintDay ) One { A intsum=0; - intmonths[ -]={0, to, -, to, -, to, -, to, to, -, to, -, to}; - if(!isrun (year)) months[2]= -; the Elsemonths[2]= in; - for(intI=1; i<=month-1; i++) - { -sum=sum+Months[i]; + } -Sum+=day;//the day ordinal of the year + returnsum; A } at intMain () - { - intYear,month,day; -scanf" %d%d%d",&year,&month,&Day ); -Cout<<daysum (Year,month,day) <<Endl; -}Topic 2: Enter two dates to output the number of days between these two date intervals.
Ideas:
- function Daysum (date): Enter the month and date of date, and the output is the number of days from date 1 January 1.
- Calculates the day of the Year in days (n)
- Statistics 1 to Year-1 days Day (n-1), common year to 365, leap year to 366
- Total days =day (n) +day (n-1)
- The number of days between date A and B, which is the absolute value of Daysum (A)-daysum (b).
1#include <iostream>2#include <stdio.h>3 using namespacestd;4 5 intIsrun (intYear )6 {7 if(((year%4==0) && (year% -!=0))|| year% -==0)return 1;8 Else return 0;9 }Ten intDaysum (intYearintMonthintDay ) One { A intsum=0; - intmonths[ -]={0, to, -, to, -, to, -, to, to, -, to, -, to}; - if(!isrun (year)) months[2]= -; the Elsemonths[2]=29; - for(intI=1; i<=month-1; i++) - { -sum=sum+Months[i]; + } -Sum+=day;//the day ordinal of the year +sum+= (year-1)*365+year/4-year/ -+year/ -;//plus the number of days from 1 to year-1 A returnsum; at } - intAbsintNum//Absolute Value function - { - if(num<0)return-num; - returnnum; - } in intMain () - { to intYear1,month1,day1,year2,month2,day2; +scanf" %d%d%d",&year1,&month1,&day1); -scanf" %d%d%d",&year2,&month2,&day2); theCout<<abs (Daysum (year1,month1,day1)-daysum (year2,month2,day2)) <<Endl; *}
Statistics date days and extended use