SWI-Prolog - 250
Oh oğlum, bunun için çok uzun zaman harcadım.
o(A,B,A+B).
o(A,B,A-B).
o(A,B,A*B).
t([],0).
t([A,B|T],D):-t(T,Q),o(A,B,C),o(C,Q,D).
t([A|T],C):-t(T,Q),o(A,Q,C).
a(A):-t(A,B),n(C),B>C,retract(n(C)),assert(n(B)).
m(A):-assert(n(0)),\+p(A),n(R),R2 is R,write(R2).
p(A):-permutation([0|A],B),a(B),0=1.
Komut satırından çağırılır (örn.):
> swipl -s filename.pl -g "m([1, 1, 1, 1, 1])" -t halt
6
(Parçasız bir sebepten dolayı, golf fonksiyonumun isimlerinin "domates saksısı" hecelediğini harika buldum.)
Ungolfed sürümü:
% Possible operations
operation(Left, Right, Left + Right).
operation(Left, Right, Left - Right).
operation(Left, Right, Left * Right).
% Possible ways to transform
transform([], 0).
transform([A, B|T], D) :- transform(T, Q), operation(A, B, C), operation(C, Q, D).
transform([A|T], C) :- transform(T, Q), operation(A, Q, C).
% Throw the given array through every possible transformation and update the max
all_transforms(A) :- transform(A, B), n(C), B>C, retract(n(C)), assert(n(B)).
% Find all the permutations and transformations, then fail and continue execution.
prog(A) :- assert(n(0)), !, permutation([0|A], B), all_transforms(B), fail.
% End the program
finished :- n(R), write(R), nl, R2 is R, write(R2), nl.
% Run the program
main(A) :- ignore(prog(A)), finished.
Açıklama:
- Bir diziyi argüman olarak kabul edin.
- Dizinin tüm permütasyonlarını alın.
- Diziye eklenecek işleçlerin bazı düzenlemelerini bulun. (Bu, ilk iki öğeyi birleştirip birleştirmemenin daha iyi olup olmadığını görerek dinamik programlama yoluyla yapılır.)
- Mevcut maksimum değerimize göre bunu kontrol edin. Daha iyiyse değiştirin.
- Programa başarısız olduğumuzu söyleyin, böylece kontrol etmeye devam eder, ancak yüklemin genel olarak geri dönmesine izin vermek için (
ignoreveya kullanarak \+)true ve devam .
- Bize bir sayı yerine bir tahmin dizisi verilir, bu yüzden bunu kullanarak atayın
isve yazın.