Ruby calculates the maximum sum of Adjacent Elements in the array.
Wei renyan 2010.8.21
I recently studied programming Pearl River and was very interested in the questions raised in the book. So according to the description in the book, I used Ruby to solve the related problems. The problems I learned today are as follows:
This is a problem in Chapter 8th of programming Pearl. Problem description: input a vector x with N floating-point numbers, output the largest sum found in any adjacent subvectors. When all input values are negative, the largest sum subvector is an empty vector, and the sum of null vectors is 0. If all input values are positive, the largest subvector is the whole input vector. Simply put, given an array a whose number is N, find the largest sum of Adjacent Elements in the array?
Example:
Array: int A [10] = [31,-41,59, 26,-53,58, 97,-93,-23,84]
Result: the maximum value of the adjacent element is a [2. 6] = 187.
The following three algorithms are provided to implement the solution. The performance of each algorithm is very different. For details, refer to the advantages and disadvantages of learning each algorithm.
Require "time" <br/> A = [31,-, 26,-, 97,-93,-] <br/> B = A [2 .. 6] <br/> sum = 0 <br/> puts. join ("") <br/> puts B. join ("") <br/> B. each do | I | <br/> sum + = I <br/> end <br/> puts sum <br/> N = 1000 <br/> # generate an array value <br/> for I in 0 .. n <br/> If I % 3 = 0 & I % 2 = 0 <br/> A [I] =-rand (100) <br/> elsif I % 4 = 0 & I % 5 = 0 <br/> A [I] =-rand (100) <br/> A [I-1] =-A [I-1] <br/> else <br/> A [I] = rand (100) <br/> end </P> <p> end <br/> puts. join ("") <br/> # first algorithm, whose performance is O (n * n) <br/> maxsofar = 0 <br/> puts "first: "<br/> Start = time. now <br/> puts "start... # {start} "<br/> for I in 0 .. n-1 <br/> # puts a [I] <br/> for J in I .. n-1 <br/> sum = 0 <br/> # puts a [J] <br/> for K in I .. j <br/> sum + = A [k] <br/> end </P> <p> If sum> maxsofar <br/> maxsofar = sum <br/> # puts "A [# {I }.. # {J}] =#{ maxsofar} "<br/> end <br/> puts maxsofar <br/> ED = time. now <br/> cs = ed-start <br/> puts "end... # {ed} "<br/> puts" consnum .. # {CS} s/n "</P> <p> # The Performance of the second algorithm is O (n * n) <br/> puts" second: "<br/> maxsofar = 0 <br/> Start = time. now <br/> puts "start... # {start} "<br/> for I in 0 .. n-1 <br/> # puts a [I] <br/> sum = 0 <br/> for J in I .. n-1 <br/> sum + = A [J] <br/> If sum> maxsofar <br/> maxsofar = sum <br/> # puts "A [# {I} .. # {J}] =#{ maxsofar} "<br/> end <br/> puts maxsofar <br/> ED = time. now <br/> cs = ed-start <br/> puts "end... # {ed} "<br/> puts" consnum .. # {CS} s/n "</P> <p> # The Performance of the third algorithm is O (n) <br/> maxsofar = 0 <br/> maxendinghere = 0 <br/> puts "Third:" <br/> Start = time. now <br/> puts "start... # {start} "<br/> for I in 0 .. n-1 <br/> If (maxendinghere + A [I])> 0 <br/> maxendinghere = (maxendinghere + A [I]) <br/> else <br/> maxendinghere = 0 <br/> end </P> <p> If maxendinghere> maxsofar <br/> maxsofar = maxendinghere <br/> # puts "A [# {I }.. # {J}] =#{ maxsofar} "<br/> end <br/> puts maxsofar <br/> ED = time. now <br/> cs = ed-start <br/> puts "end... # {ed} "<br/> puts" consnum .. # {CS} s"
Note: If you need to reprint the record, please indicate the source. Thank you!
Http://blog.csdn.net/savechina