fork download
  1. def getMax(level)
  2. max = 0
  3. for i in 1..level-1
  4. if $st[i] > max
  5. max = $st[i]
  6. end
  7. end
  8. return max
  9. end
  10.  
  11. def init(level)
  12. $st[level] = 0
  13. end
  14.  
  15. def succ(level)
  16. if $st[level]<getMax(level)+1
  17. $st[level]+=1
  18. return true
  19. end
  20. return false
  21. end
  22.  
  23. def valid(level)
  24. return true
  25. end
  26. def sol(level)
  27. return $n == level
  28. end
  29.  
  30. def printf()
  31. $cnt +=1
  32. print $cnt
  33. takemax = $st.max()
  34. for i in 1..takemax
  35. print "{"
  36. for j in i..$n
  37. if $st[j] == i
  38. print j, ''
  39. end
  40. end
  41. print "}"
  42. end
  43. print"\n"
  44. end
  45.  
  46. def bk(level)
  47. init(level)
  48. while succ(level)
  49. if valid(level)
  50. if sol(level)
  51. printf()
  52. else
  53. bk(level+1)
  54. end
  55. end
  56. end
  57. end
  58. def partitions
  59. $cnt = 0
  60. $n = gets.chomp.to_i
  61. $st = [0] * ($n+1)
  62. $st[1] = 1
  63. bk(2)
  64. end
  65. partitions
  66.  
Success #stdin #stdout 0.01s 6264KB
stdin
5
stdout
1{12345}
2{1234}{5}
3{1235}{4}
4{123}{45}
5{123}{4}{5}
6{1245}{3}
7{124}{35}
8{124}{3}{5}
9{125}{34}
10{12}{345}
11{12}{34}{5}
12{125}{3}{4}
13{12}{35}{4}
14{12}{3}{45}
15{12}{3}{4}{5}
16{1345}{2}
17{134}{25}
18{134}{2}{5}
19{135}{24}
20{13}{245}
21{13}{24}{5}
22{135}{2}{4}
23{13}{25}{4}
24{13}{2}{45}
25{13}{2}{4}{5}
26{145}{23}
27{14}{235}
28{14}{23}{5}
29{15}{234}
30{1}{2345}
31{1}{234}{5}
32{15}{23}{4}
33{1}{235}{4}
34{1}{23}{45}
35{1}{23}{4}{5}
36{145}{2}{3}
37{14}{25}{3}
38{14}{2}{35}
39{14}{2}{3}{5}
40{15}{24}{3}
41{1}{245}{3}
42{1}{24}{35}
43{1}{24}{3}{5}
44{15}{2}{34}
45{1}{25}{34}
46{1}{2}{345}
47{1}{2}{34}{5}
48{15}{2}{3}{4}
49{1}{25}{3}{4}
50{1}{2}{35}{4}
51{1}{2}{3}{45}
52{1}{2}{3}{4}{5}