File tree 2 files changed +61
-0
lines changed
2 files changed +61
-0
lines changed Original file line number Diff line number Diff line change
1
+ #问题
2
+
3
+ 将一个整数,分拆为若干整数的和。例如实现:
4
+ 4=3+1
5
+ 4=2+2
6
+ 4=2+1+1
7
+ 4=1+1+1+1
8
+
9
+ #解决(Python)
10
+
11
+ #! /usr/bin/env python
12
+ #encoding:utf-8
13
+
14
+ """
15
+ """
16
+
17
+ def int_divided(m,r,out_list):
18
+ if(r==0):
19
+ return True
20
+ m1=r
21
+ while m1>0:
22
+ if(m1<=m):
23
+ out.append(m1)
24
+ if(divide(m1,r-m1,out)):
25
+ print out
26
+ out.pop()
27
+ m1-=1
28
+ return False
29
+
30
+
31
+ n=6
32
+ output=[]
33
+ int_divided(n-1,n,output)
Original file line number Diff line number Diff line change
1
+ #! /usr/bin/env python
2
+ #encoding:utf-8
3
+
4
+ """
5
+ 将一个整数,分拆为若干整数的和。例如实现:
6
+ 4=3+1
7
+ 4=2+2
8
+ 4=2+1+1
9
+ 4=1+1+1+1
10
+ """
11
+
12
+ def divide (m ,r ,out ):
13
+ if (r == 0 ):
14
+ return True
15
+ m1 = r
16
+ while m1 > 0 :
17
+ if (m1 <= m ):
18
+ out .append (m1 )
19
+ if (divide (m1 ,r - m1 ,out )):
20
+ print out
21
+ out .pop ()
22
+ m1 -= 1
23
+ return False
24
+
25
+
26
+ n = 6
27
+ out = []
28
+ divide (n - 1 ,n ,out )
You can’t perform that action at this time.
0 commit comments