fork download
  1. def func
  2.  
  3. def init
  4. if $level == 1
  5. $stack[$level] = 0
  6. else
  7. $stack[$level] = $stack[$level-1]-1
  8. end
  9. end
  10.  
  11. def succ()
  12. if $stack[$level] < $n - $s
  13. $stack[$level] += 1
  14. return true
  15. else
  16. $s = $s - $stack[$level-1]
  17. return false
  18. end
  19. end
  20.  
  21. def valid
  22. if $stack[$level] <= $n - $s
  23. $s = $s + $stack[$level]
  24. return true
  25. end
  26. return false
  27. end
  28.  
  29. def sol
  30. return $s == $n
  31. end
  32.  
  33. def printf
  34. for i in 1..$level
  35. print $stack[i], ' '
  36. end
  37. $s = $s - $stack[$level]
  38. print "\n"
  39. end
  40.  
  41. def solve
  42. $s = 0
  43. $level = 1
  44. init()
  45.  
  46. while $level > 0
  47. su = true
  48. v = false
  49. while su == true && v == false
  50. su = succ()
  51. if su == true
  52. v = valid()
  53. end
  54. if su == true
  55. if sol() == true
  56. printf()
  57. else
  58. $level = $level + 1
  59. init()
  60. end
  61. else
  62. $level = $level - 1
  63. end
  64. end
  65. end
  66. end
  67.  
  68. $n = 5
  69. $stack = [0] * ($n+1)
  70. solve
  71. end
  72.  
  73. func
  74.  
Success #stdin #stdout 0.01s 6276KB
stdin
Standard input is empty
stdout
1 1 1 1 1 
1 1 1 2 
1 1 3 
1 2 2 
1 4 
2 3 
5