--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 ** maximum array value **.
--Set the first element of the array as the temporary maximum value. The remaining elements of the array are compared in order with the tentative maximum, and if a larger value is found, the tentative maximum is updated.
#Max function to find the maximum value of an array
def Max(A,Length):
Ans = A[0]
#Iterative processing
i = 1 #Set the initial value of the loop counter to 1.
while i < Length:
print("Ans=",Ans,"i=",i,"A[i]=",A[i])
#Branch processing
if A[i] > Ans: #If the newly extracted element is larger than the maximum value
Ans = A[i] #Update the tentative maximum
i+=1
return Ans
print("Execution result:",Max([12,56,78,34,90],5))
Ans= 12 i= 1 A[i]= 56
Ans= 56 i= 2 A[i]= 78
Ans= 78 i= 3 A[i]= 34
Ans= 78 i= 4 A[i]= 90
Execution result: 90
--The maximum value of the array is unexpectedly difficult ――Next time, let's write an algorithm for ** linear search **
--I quoted or referred to the maximum value of the Chapter 3 03 array in this book. Information processing textbook, book that can solve algorithm problems of the Fundamental Information Technology Engineer Examination, 2nd edition
Recommended Posts