--There is an algorithm in the afternoon exam of the Fundamental Information Technology Engineer Examination. I can't understand even if I solve the past questions ... I would like to actually write the algorithm in Python to deepen my understanding.
--Last time, I wrote the algorithm of Euclidean algorithm. --This time, I will write from the algorithm of ** leap year judgment **.
--If the Christian era is not divisible by 4 and 100, it is a leap year. However, if the Christian era is divisible by 400, it is a leap year. If neither is true, it is not a leap year.
#IsLeapYear function to determine leap year
def IsLeapYear(Year):
#Outer branch processing
if Year % 4 == 0 and Year % 100 != 0: #If this condition is true
Ans = True #Leap year
else:
#Inner branch processing
if Year % 400 == 0: #If this condition is true
Ans = True #Leap year
else:
Ans = False #Not a leap year
return Ans
print("Execution result:",IsLeapYear(2104))
print("Execution result:",IsLeapYear(2105))
print("Execution result:",IsLeapYear(2200))
print("Execution result:",IsLeapYear(2400))
Execution result: True
Execution result: False
Execution result: False
Execution result: True
――Judgment of leap year is unexpectedly complicated. --Next time, let's write an algorithm for ** maximum array value **
--I quoted or referred to the judgment of Chapter 3 02 leap year in this book. Information processing textbook, book that can solve algorithm problems of the Fundamental Information Technology Engineer Examination, 2nd edition
Recommended Posts