루비, 118114 (배열 출력) 또는 138134 (정확한 출력)
i=gets.to_i
a=[x=y=1]
a+=[y=x+x=y]until y>i
p (1..a.size).flat_map{|n|a.combination(n).select{|o|o.inject(:+)==i}}
샘플 실행 :
c:\a\ruby>fibadd
100
[[3, 8, 89], [1, 2, 8, 89], [3, 8, 34, 55], [1, 2, 3, 5, 89], [1, 2, 8, 34, 55], [3, 8, 13, 21, 55], [1, 2, 3, 5, 34, 55], [1, 2, 8, 13, 21, 55], [1, 2, 3, 5, 13, 21, 55]]
변경 gets
에 $*[0]
당신이 명령 줄 인수 (원하는 경우 >fibadd 100
)하지만 한 문자.
올바른 출력으로 :
i=gets.to_i
a=[x=y=1]
a+=[y=x+x=y]until y>i
$><<(1..a.size).flat_map{|n|a.combination(n).select{|o|o.inject(:+)==i}}.map{|o|o*?+}*'
'
샘플 실행 :
c:\a\ruby>fibadd
100
3+8+89
1+2+8+89
3+8+34+55
1+2+3+5+89
1+2+8+34+55
3+8+13+21+55
1+2+3+5+34+55
1+2+8+13+21+55
1+2+3+5+13+21+55
c:\a\ruby>fibadd
1000
13+987
5+8+987
13+377+610
2+3+8+987
5+8+377+610
13+144+233+610
2+3+8+377+610
5+8+144+233+610
13+55+89+233+610
2+3+8+144+233+610
5+8+55+89+233+610
13+21+34+89+233+610
2+3+8+55+89+233+610
5+8+21+34+89+233+610
2+3+8+21+34+89+233+610
c:\a\ruby>obfcaps
12804
2+5+21+233+1597+10946
2+5+8+13+233+1597+10946
2+5+21+89+144+1597+10946
2+5+21+233+610+987+10946
2+5+21+233+1597+4181+6765
2+5+8+13+89+144+1597+10946
2+5+8+13+233+610+987+10946
2+5+8+13+233+1597+4181+6765
2+5+21+34+55+144+1597+10946
2+5+21+89+144+610+987+10946
2+5+21+89+144+1597+4181+6765
2+5+21+233+610+987+4181+6765
2+5+8+13+34+55+144+1597+10946
2+5+8+13+89+144+610+987+10946
2+5+8+13+89+144+1597+4181+6765
2+5+8+13+233+610+987+4181+6765
2+5+21+34+55+144+610+987+10946
2+5+21+34+55+144+1597+4181+6765
2+5+21+89+144+233+377+987+10946
2+5+21+89+144+610+987+4181+6765
2+5+21+233+610+987+1597+2584+6765
2+5+8+13+34+55+144+610+987+10946
2+5+8+13+34+55+144+1597+4181+6765
2+5+8+13+89+144+233+377+987+10946
2+5+8+13+89+144+610+987+4181+6765
2+5+8+13+233+610+987+1597+2584+6765
2+5+21+34+55+144+233+377+987+10946
2+5+21+34+55+144+610+987+4181+6765
2+5+21+89+144+233+377+987+4181+6765
2+5+21+89+144+610+987+1597+2584+6765
2+5+8+13+34+55+144+233+377+987+10946
2+5+8+13+34+55+144+610+987+4181+6765
2+5+8+13+89+144+233+377+987+4181+6765
2+5+8+13+89+144+610+987+1597+2584+6765
2+5+21+34+55+144+233+377+987+4181+6765
2+5+21+34+55+144+610+987+1597+2584+6765
2+5+21+89+144+233+377+987+1597+2584+6765
2+5+8+13+34+55+144+233+377+987+4181+6765
2+5+8+13+34+55+144+610+987+1597+2584+6765
2+5+8+13+89+144+233+377+987+1597+2584+6765
2+5+21+34+55+144+233+377+987+1597+2584+6765
2+5+8+13+34+55+144+233+377+987+1597+2584+6765
마지막 것 (12804)은 약 3 초 밖에 걸리지 않았습니다!