Home > Back-end >  How many days
How many days

Time:04-03

7-4 day-of-week (function)
Please write a function, form parameters for the said date of three integers (to date), return to this day is what day of the year,

The main function of the input year and two months, a total of five integers, said two date, output the two date by how many day,

Input format:
Y M1, M2 D1, D2, a total of five integers, Y years, M1 D1 said of the date of the first month, the M2 D2 said the second date day of month, all the data to ensure date one before, and 2 in the date,

Input the sample:
2021 1 1 1 31
The output sample:
30
Input the sample:
2020 1 2 3 1
The output sample:
29
Input the sample:
2020 1 1 December 31
The output sample:
365
Input the sample:
2021 1 1 December 31
The output sample:
364

CodePudding user response:

Reference:
 # include & lt; Stdio. H> 

31,29,31,30,31,30,31,31,30,31,30,31 int [12] s={};
Int f [12]={31,28,31,30,31,30,31,31,30,31,30,31};

Bool isLeap (int year)//determine whether for leap years
{
If ((year % 4==0 & amp; & Year % 100!=0) | | (400 year %==0))
return 1;
The else
return 0;
}

During the int isLegal (int, int the month, int day//whether the date legal
{
Int j=0;
If (month> 0 & amp; & Month<=12 & amp; & Day> 0 & amp; & Day<31)={
If (isLeap (year)) {
If (day & lt;=s [] the month - 1) j=1;
}
The else {
If (day & lt; J=f] [the month - 1)=1;
}
}
Return j;
}

During the int Num (int, int the month, int day//statistical number
{
Int d=day;
If (isLeap (year)) {
for(int i=0; iD +=s [I];
}
The else {
for(int i=0; iD +=f [I];
}
Return d;
}

Int main (int arg c, char * argv [])
{
Int year, month, day, month1, day1.

Do {//whether input the date on which the legal
Printf (" please enter the correct date format (yyyy - mm - dd) : ");
4 the scanf (" % d % 2 d - % 2 d ", & amp; Year, & amp; The month, & amp; Day);
} while (! IsLegal (year, month, day);
Printf (" the date is the year's first day % d \ n ", Num (year, month, day);

Printf (" please enter the year and two months (Y M1 M2 D1 D2) : ");
4 the scanf (" % d % 2 d % 2 d % 2 d % 2 d ", & amp; Year, & amp; The month, & amp; Day, & amp; Month1, & amp; Day1);
Printf (" % d \ n ", Num (year, month1, day1) - Num (year, month, day);

return 0;
}

CodePudding user response:

Can calculate after subtraction can,

CodePudding user response:

Write complicated, first of all, the subject said the legal guarantee date, without judgment, their own
The O (1) algorithm (!!!!!! On the blackboard!!!!!! 90009 students listen ) :
 
#include

Int o (int y, int m, int d)
{
Int y1=y;

M=(m + 9) % 12, y=y -m/10;
Return y * 365 + 100 + 4 - y/y/y/400 + (m * 306 + 5)/10 + d -
Y1 * 365 - y1/4 + y1/100 - y1/400-31;
}

Int main ()
{
Int y, m1, d1, m2, d2;

The scanf (" % d % d % d % d % d ", & amp; Y, & amp; M1, & amp; D1, & amp; M2, & amp; D2);
Printf (" % d \ n ", o (y, m2, d2) - o (y, m1, d1));

return 0;
}

CodePudding user response:

reference 3 floor early play play nuclear response:
write complicated, first of all, the subject said the legal guarantee date, without judgment, their own
The O (1) algorithm (!!!!!! On the blackboard!!!!!! 90009 students listen ) :
 
#include

Int o (int y, int m, int d)
{
Int y1=y;

M=(m + 9) % 12, y=y -m/10;
Return y * 365 + 100 + 4 - y/y/y/400 + (m * 306 + 5)/10 + d -
Y1 * 365 - y1/4 + y1/100 - y1/400-31;
}

Int main ()
{
Int y, m1, d1, m2, d2;

The scanf (" % d % d % d % d % d ", & amp; Y, & amp; M1, & amp; D1, & amp; M2, & amp; D2);
Printf (" % d \ n ", o (y, m2, d2) - o (y, m1, d1));

return 0;
}

Have a lazy, friendship deliver a function
  • Related