루아, (324) 323 바이트
다른 제출을 볼 때 코드에 문제가있는 것 같습니다 ...하지만 Lua라는 것을 기억하고 이러한 멋진 기능이 모두 없습니다 : '(
그것은 많은 재미이었다, 실제로 시간이 좀 걸렸다.
편집 : 간단한 트릭으로 1 바이트를 절약했습니다. ::label::+ goto label대신 무한 루프를 사용하십시오 while''.
function f(l)c=2 y,z=table.remove,os.exit while(l[1]<2)do y(l,1)if(#l<1)then print(1)z()end end ::q:: a={}for i=1,c do a[i]=1 end b={}for i=1,#l do b[i]=l[i]end while(a[#a]<b[1])do x=0 for i=(#a-c+1>0 and #a-c+1 or 1),#a do x=x+a[i]end a[#a+1]=x if a[#a]==b[1]then y(b,1)end if #b<1 then print(c)z()end end c=c+1 goto q end
언 골프 및 설명
Lua는 집합 / 집합을 정의하거나 배열 / 테이블에 색인 / 키를 사용하지 않고 값이 포함되어 있는지 확인할 방법이 없습니다. 그것이 내가 매개 변수로 취하는 배열에서 요소를 제거하기로 결정한 이유입니다. 이것이 내가 이미 계산 한 요소와 일치하는 요소를 기록하는 방법입니다.
function f(l)
c=2
y,z=table.remove,os.exit -- Create pointers on table.remove and os.exit
-- saves a total of 9 bytes
while(l[1]<2) -- loop used to remove leading 1
do
y(l,1)
if(#l<1) -- we also check if it was a 1-only array
then
print(1) -- if so, we print 1 and exit
z()
end
end
::q:: -- label q, start of the infinite loop
a={}for i=1,c do a[i]=1 end -- fill an array with c 1s
b={}for i=1,#l do b[i]=l[i]end -- copy the sequence array
while(a[#a]<b[1]) -- while max(a)<min(b)
do
x=0 for i=(#a-c+1>0 -- iterate from index a.length-c to
and #a-c+1 -- to a.length
or 1),#a
do
x=x+a[i] -- summing a's elements
end
a[#a+1]=x -- append x to a
if a[#a]==b[1]then y(b,1)end -- if x is equal ot a member of the sequence
-- remove it
if #b<1 then print(c)z()end -- if b is empty, it means the subset is in a
-- we print c and exit
end -- else we loop again
c=c+1 -- with c+1
goto q -- return to the start of this block
end
Lua online을 사용해 볼 수 있으며 다음 코드 샘플을 복사 / 붙여 넣기하여 테스트를 실행할 수 있습니다. 이 함수가 답을 찾으면 종료되므로 (그렇지 않으면 무한 루프), test[]사용 된 인덱스를 변경해야합니다 (루아가 1 인덱싱 된 것을 잊지 마십시오 :).
function f(l)c=2 y,z=table.remove,os.exit while(l[1]<2)do y(l,1)if(#l<1)then print(1)z()end end ::q:: a={}for i=1,c do a[i]=1 end b={}for i=1,#l do b[i]=l[i]end while(a[#a]<b[1])do x=0 for i=(#a-c+1>0 and #a-c+1 or 1),#a do x=x+a[i]end a[#a+1]=x if a[#a]==b[1]then y(b,1)end if #b<1 then print(c)z()end end c=c+1 goto q end
test={{1,1,1},
{49, 97},
{55, 89, 144},
{1},
{6765},
{12, 23, 45, 89},
{100, 199}}
print(f(test[1]))
create a black hole (as long as this black hole does not produce anything that could be mistaken for valid output).블랙홀의 나선은 황금 비율로 수렴하고 있습니다! 그것은 해야한다 duoacci 순서 유효 출력!