Home>

I would like to find the number of days elapsed from January 1, 1900 to January 1, X, taking into account the leap year.

If i compare the results with this site, it may sometimes be off by one day. I don't know where the problem is. Corresponding source code
#include<iostream>
int main ()
{
  int year;
  std :: cin >>year;
  int day {0};
  int a, b, c, d, e;
  a = year-1900;
  b = 365 * a;
  c = a/4;
  d = a/100;
  e = a/400;
  day + = b;
  day + = c;
  day-= d;
  day + = e;
  std :: cout<<day;
  return 0;
}
c++
  • Answer # 1

    Although it depends on the answers and contents of other people

    Years that are divisible by 400 after 1900 are, for example, 2000, 2400.
    However, if you judge whether it is divisible by 400 after subtracting 1900, 2300, 2700 ,,, will be applicable, and since it is divisible by 400, the year to add one day to become a leap year will be 300 years different from the actual year.

  • Answer # 2

    The leap year calculation is strange
    1900 is not divisible by 400

    Let's check the definition of leap year

  • Answer # 3

    I don't know where the problem is.

    At least one point. The year 2000 is a leap year because it is divisible by 400. Therefore, entering 2001 should add one day. However, in the code

    a = year --1900;// 2001 --1900 = 101
    ...
    e = a/400;
    ...
    day + = e;


    Since it is, one day of the leap year of 2000 will not be added.

    On the referenced site, you can select either "Do not include or include the first day", but I do not know which one the questioner's code wants to fit, so I will only answer the above points.

  • Answer # 4

    Try changing it as follows:

    --c = a/4;
    --d = a/100;
    --e = a/400
    + c = (a + 3)/4;
    + d = (a + 99)/100;
    + e = (a + 299)/400;

    Do you get the results you expect?