标签:range instance div join() thread replace mon false count
import randomimport threadingN = 100res_dic = {}def compute_combination(n, x): numerator = 1 denominator = 1 breaker = n - x for i in range(n, breaker, -1): numerator *= i breaker = x + 1 for i in range(1, breaker, 1): denominator *= i return int(numerator / denominator)def gen_column(num, newer): random_breaker = N - 1 replace_index = random.randint(0, random_breaker) if newer[replace_index] == 0: newer[replace_index] = 1 if newer.count(0) == num: return newer else: gen_column(num, newer)def enumerate_column(num): combination = compute_combination(N, num) i = 0 while i < combination: model_list = [0 for ii in range(0, N, 1)] gen_column(num, model_list) if model_list in res_dic[num]: i -= 1 if model_list not in res_dic[num]: res_dic[num].append(model_list) if len(res_dic[num]) == combination: return res_dic[num] i += 1 print(num, len(res_dic[num]), combination)NO_LIST = [95, 85, 80, 79, 74]for i in NO_LIST: res_dic[i] = []class MyThread(threading.Thread): def __init__(self, func, args, name): threading.Thread.__init__(self) self.name, self.func, self.args = name, func, args def run(self): self.func(self.args)threads_list = []for i in res_dic: thread_instance = MyThread(enumerate_column, (i), enumerate_column.__name__) threads_list.append(thread_instance)for t in threads_list: t.setDaemon = False t.start()for t in threads_list: t.join()a, b, c, d, e = res_dici, min_, target_ = 0, N, 3for ai in res_dic[a]: for bi in res_dic[b]: for ci in res_dic[c]: for di in res_dic[d]: for ei in res_dic[e]: final_dic = {} for ii in range(0, 6, 1): final_dic[ii] = 0 for ii in range(0, N, 1): l_ = [] l_.append(ai[ii]) l_.append(bi[ii]) l_.append(ci[ii]) l_.append(di[ii]) l_.append(ei[ii]) count_ = l_.count(1) final_dic[count_] += 1 compare_ = 0 for ii in range(0, target_, 1): compare_ += final_dic[ii] min_ = min(min_, compare_)print(min_)
标签:range instance div join() thread replace mon false count
原文地址:http://www.cnblogs.com/yuanjiangw/p/7355228.html