亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

python實現(xiàn)正整數(shù)N等于M個正整數(shù)之和

系統(tǒng) 2239 0

#_*_ coding: UTF-8 _*_
#正整數(shù)N等于M個正整數(shù)之和

import itertools
import sys
import os?
import copy

#參數(shù)說明
#total:表示有多少個數(shù),M
#sum:表示正整數(shù)的和,N
#comb_list:存儲最終輸出的所有列表組合
#tmp_comb_list:臨時最終輸出的所有列表組合
#max_num:當前組合中的最大取值
#num_pos:當前求出的數(shù)值在當前l(fā)ist中保存的位置
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)


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 奇米影视7777久久精品人人爽 | 久久狠狠婷婷丁香香蕉 | 欧美精品中文字幕手机免费视频 | 四虎w345com| 99久久免费精品国产免费 | 99热久久国产精品这里 | 亚洲好视频 | 欧美又黄又嫩大片a级 | 国产成人无精品久久久 | 91精品久久久 | 午夜免费福利不卡网址92 | 天天综合亚洲国产色 | 国产成人毛片亚洲精品不卡 | 国产精品成人一区二区不卡 | 麻豆首页 | 99爱在线精品视频网站 | 亚洲偷自拍另类图片二区 | 国产亚洲精品久久久久久久软件 | 久久久久免费观看 | 日本韩国欧美一区 | 久久成人免费 | 天天天天天操 | 中国国产一国产一级毛片视频 | 神马影院在线观看我不卡 | 日本aⅴ网站 | 完整日本特级毛片 | 九九视频免费精品视频免费 | 鲁鲁狠色综合色综合网站 | 91在线播放视频 | 2018久久久国产精品 | 欧美成人七十二式性视频教程 | 中文字幕 亚洲精品 第1页 | 日本一二三区免费 | 国产成人午夜性视频影院 | 8090碰成年女人免费碰碰尤物 | 久久精品亚洲一级毛片 | 日本美女视频韩国视频网站免费 | 四虎免费永久观看 | 亚洲综合日韩中文字幕v在线 | 在线观看免费视频a | 中文字幕精品一区二区三区在线 |