-
Here, 146097 = 365 * 400 + 97. That is, the total number of days in 400 years, because 400 years 97 leaps.
1461 = 365 * 4 + 1, that is, the total number of days in 4 years, here it means a leap in 4 years.
ldate is the total number of days, ( ldate ) 146097 has gone through several 400 years.
ldate ) 146097 * 97 ) The number of leap years in 400 years that is an integer multiple.
ldate % 146097 ) is the number of days remaining in the total number of days in 400 years minus an integer multiple.
ldate % 146097 ) 1461 went through several leap years.
-
Enter the three values of year, month and day, first add the number of days per month in each month before the year, and finally add the number of days of the date to obtain the result.
1. Algorithm design
1. Enter three values of year, month and day;
2. In order to ensure the robustness of the program, the legitimacy of the three values can be judged, and only the legal data can be operated;
3. The number of days in each month before accumulation;
4. Accumulate the current daily value;
5. Output the result.
2. Points to note:
You can use an array to store the number of days in each month, adding them up in a loop. It is important to note the judgment of leap years. For leap years, an extra day is added in February.
#include
int main()
Number of days in twelve months.
int y,m,d,r=0,i;
scanf("%d%d%d",&y,&m,&d);
if(y%400==0 ||y%4==0 &&y %100 != 0)) Leap year.
mdays[1] = 29;Set the February value for leap years.
if(y<0 ||m<=0 ||m>12 ||d<=0 ||d>mdays[m-1]) is invalid.
for(i = 0; i < m-1;I++ accrues to the previous months.
r+=mdays[i];
r+=d;Accumulate date values.
printf("%d",r);Output the result.
return 0;}
-
The program is good, but the mistake is that the following judgment of leap year does not work on the calculation results, so just change one place:
The penultimate sentence will be counted.
if(er==1&&month>2);
Changed to: if(month>2).
sum+=er;Can.
Year, Month and Day Knowledge Points:
There are 12 months in a year. >>>More
The year first came from agriculture, and in ancient times, people called the growth cycle of the valley as the year, which originated in the Zhou Dynasty, and was officially fixed in the Western Han Dynasty, and then continued to this day. >>>More
First of all, add the date number you want to know, add the month code, and divide it by 7, which is divisible by Sunday; If it is not divisible, the remainder is the number, and the day is the day of the week. If the sum of the date and month codes is less than 7, then the sum is the number of days, and that day is the day of the week. For example: >>>More
Year: year
Month: month >>>More