#_*_ coding: UTF-8 _*_
#正整数N等于M个正整数之和
import itertools
import sys
import os
import copy
#参数说明
#total:表示有多少个数,M
#sum:表示正整数的和,N
#comb_list:存储最终输出的所有列表组合
#tmp_comb_list:临时最终输出的所有列表组合
#max_num:当前组合中的最大取值
#num_pos:当前求出的数值在当前list中保存的位置
def doFindComb(total, sum, comb_list, tmp_comb_list, max_num=1, num_pos=0):
tmp_result = copy.deepcopy(tmp_comb_list)
if(total > sum):
return
elif(total == sum):
for i in range(num_pos, total+num_pos):
tmp_result[i]=1
result_data = list(itertools.permutations(tmp_result))
list_len = len(result_data)
for i in range(0, list_len):
result_data[i] = list(result_data[i])
for item in result_data:
if item not in comb_list:
comb_list.append(item)
return
elif(total == 1):
if(sum <= max_num):
tmp_result[num_pos]=sum
result_data = list(itertools.permutations(tmp_result))
list_len = len(result_data)
for i in range(0, list_len):
result_data[i] = list(result_data[i])
for item in result_data:
if item not in comb_list:
comb_list.append(item)
return
min = int(sum/total)
if(sum % total != 0):
min = int(sum/total) +1
max = sum-total+1
tmp_list = [0 for x in range(0, total+num_pos)]
for i in range(min, max+1):
tmp_list = copy.deepcopy(tmp_comb_list)
tmp_list[num_pos]=i
doFindComb(total-1, sum-i, comb_list, tmp_list, i, num_pos+1)
'''
comb_list = []
tmp_comb_list = [0 for x in range(0, total)]
total = 5
sum = 20
doFindComb(total, sum, comb_list, tmp_comb_list)
'''
if __name__ == "__main__":
if (len(sys.argv) != 3):
print("parameter error")
print("usage: python xxx.py m n")
else:
total = int(sys.argv[1])
sum = int(sys.argv[2])
comb_list = []
tmp_comb_list = [0 for x in range(0, total)]
doFindComb(total, sum, comb_list, tmp_comb_list)
print(comb_list)