A - Leap Year Editorial by en_translator
For beginners
- If you are new to learning programming and do not know where to start, please try Problem A "Welcome to AtCoder" from practice contest. There you can find a sample code for each language.
- Also, if you are not familiar with problems in programming contests, we recommend you to try some problems in "AtCoder Beginners Selection".
- 「C++入門 AtCoder Programming Guide for beginners (APG4b)」 is a C++ tutorial for competitive programmers. Sadly, this is only in Japanese too.
- 「Python入門 AtCoder Programming Guide for beginners (APG4bPython)」 is a Python tutorial for competitive programmers. Again, this is only in Japanese.
Let us use an if
statement to implement the casework in the problem statement.
To determine if \(x\) is divisible by \(y\), one can check if x % y == 0
. Here, x % y
denotes the remainder when \(x\) is divided by \(y\), where %
is the operator that finds the remainder.
When \(x\) is divisible by \(y\), the remainder when \(x\) is divided by \(y\) is \(0\), so the code above determines the divisibility.
In our case, there are multiple if
statements, possible making the code deeply nested. In such case, one can immediately print the result and return
once a condition is met to avoid deep nests.
Sample code (C++):
#include <bits/stdc++.h>
using namespace std;
int main() {
int y;
cin >> y;
if(y % 4) {
cout << 365 << endl;
return 0;
}
if(y % 100) {
cout << 366 << endl;
return 0;
}
if(y % 400) {
cout << 365 << endl;
return 0;
}
cout << 366 << endl;
return 0;
}
posted:
last update: