Statistics date days and extended use

Source: Internet
Author: User

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:
    1. Can be divisible by 4 but not divisible by 100 (for example, 2004 is leap year, 1800 is not)
    2. 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.
    1. Calculates the day of the Year in days (n)
    2. Statistics 1 to Year-1 days Day (n-1), common year to 365, leap year to 366
    3. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.