-
Your logic is not wrong.
There's a slight mistake with your format: scanf("%d,%d,%d", y, m, d) should be replaced by :
scanf("%d%d%d",&y,&m,&d);
-
To calculate the number of days between two years, months and days, the idea is to calculate the total number of days on each day and subtract them.
It is necessary to consider the situation of leap years, and judge the formula for leap years: every leap in 4 years, no leap in 100 years, and then in 400 years.
year % 4 == 0 &&year % 100 != 0) |year % 400 == 0)
I found one on the net (lazy = =!), modified as follows:
#include
int sum(int y,int m,int d);int i,s=0;
for(i=1;i s2)
printf("Number of days apart: %ld",s1-s2);
elseprintf("Number of days apart: %ld",s2-s1);
The above **VC6 compilation test passed.
Although this idea seems a bit stupid, other algorithms, ** are too long and complicated, and it is better to directly calculate the number of days between the two dates and January 1 of the first year of the Common Era, and then subtract them.
-
Hello! Your program is working fine, it's not because there is a problem with the format when you enter the data.
-
[Procedure 1]:
main()
sum=sum+day;*Plus the number of days on a given day* to determine if it is a leap year*
leap=1;
elseleap=0;
if(leap==1&&month>2) *If it is a leap year and the month is greater than 2, the total number of days should be added by one day*
sum++;
printf("it is the %dth day.",sum);}
-
[Procedure 1]:
main()
sum=sum+day;*Plus the number of days on a given day* to determine if it is a leap year*
leap=1;
elseleap=0;
if(leap==1&&month>2) *If it is a leap year and the month is greater than 2, the total number of days should be added by one day*
sum++;
printf("it is the %dth day.",sum);}
-
The format is separated by commas, but "2011 3 17" is separated by spaces, so the input does not get the correct value, and the subsequent calculation results are bound to be wrong.
-
I haven't used C for a long time, and I remember that the input data should be like this: 2011, 3, 17. I don't know if I'm mistaken.
-
Both programs calculate the correct number of days.
ps: There is a small mistake in the first part of the program, sum+=a[i]; and sum+=b[i]; , i should be replaced by j
However, because the second part of the program has to judge if(leap(year)) many times, that is, it needs to execute the function int leap(int a) many times. In the first segment, the function int leap(int a) is executed only once. Therefore, the efficiency of program segment 1 is higher than program segment 2.
Test**: include
int leap(int year)
main()
int b[12]=;
int j,sum,year,month,day;
first paragraph * sum=0;
if(leap(year))
for(j=1;The number of days for the calculation of the results of the jrun is the same.
-
The second one is definitely wrong, the difference between a leap year and a peaceful year is that February is 28, 29
But the second program, which adds 1 more every month
for(j=1;j
-
The subscript of the array in the second segment of the program is incorrect.
-
Put int caldays function.
if(year1=year2) is changed to if(year1==year2).
This is the error, and it should be possible to find it by visual inspection after correcting it.
*Find 2+4+6+8+....+100 and *
#include "" >>>More
1 Don't give up, think slowly.
2 Don't forget that C++ and C can be compared to each other, the biggest difference is only in the output part. >>>More
Mistake 1: Because the three-digit number you are looking for is an integer, all variables should be int and not float. >>>More
int max(float fx,float fy)float fz;
fz=fx>fy?fx:fy; >>>More