-2
我想寫在Haskell這個方法:獲得價值
private static int maxSubSumme(int array[])
{
int maximum = 0;
int maxright = 0;
for(int i=array.length-1; i > -1; i--)
{
maxright = Math.max(maxright + array[i], 0);
maximum = Math.max(maximum, maxright);
}
return maximum;
}
我這個代碼的嘗試:
maxSubSumme :: [Int] -> Int
maxSubSumme [x] = x
maxSubSumme (x:l) | maxright < maximum = maximum
| maxright > maximum = maxright
where
maxright = maxi (x + (maxSubSumme l)) 0
maximum = maxi maximum maxright -- Here is my problem
maxi :: Int -> Int -> Int
maxi x y | x > y = x
| otherwise = y
我得老最大的,但我不知道我怎麼能做到這一點。是否有可能從上次函數調用中獲得最大值?
你可以擴展的代碼一點點?我沒有看到這個代碼中應該包含什麼oldRest或者如何使用其餘函數。 – Malcolm
你的代碼沒有意義。你在說'休息'是一些價值和本身的最大值。所以你有一個值的遞歸定義,如果不假設'rest'的某個初始值,那麼這個值是不可能解決的。 – DiegoNolan
我編輯我的問題;) – Cilenco