11. Year

topic:

Enter the year, to determine whether a leap year. If so, then the output yes, otherwise output no. Tip: Simply determine the remainder divided by four is not enough.

Ideas:

Leap year judging method, in which one of the following two conditions is leap year: (1) a multiple of 4, but not a multiple of 100, i.e., (year% 4 == 0) && (year% 100 = 0! )

                            (2) is a multiple of 400, i.e., (year% 400 == 0)

Here we must understand the difference between ordinary and century leap year leap year. The first is used to determine normal leap year, it is used to determine the second century leap years. As can be seen, if the year is 2000, the first is not satisfied, because the 2000 and 2000% 100% 4 are 0,

But in fact 2000 is a leap year, so it needs to satisfy the second condition.

Code:

#include <iostream>
using namespace std;

int main()
{
int year = 0;
cin >> year;

if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
cout << "yes" << endl;
} else {
cout << "no" << endl;
}

return 0;
}

Guess you like

Origin www.cnblogs.com/Hello-Nolan/p/12110467.html