本文主要是介绍ahalei_14,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
14.请将1~9这9个阿拉伯数字分别填入9个□中。每个数字只能使用一次。使得等式成立。
□□□+□□□=□□□
例如173+286=459就是一个合理的组合,请问一共所有少种合理的组合?
注意:173+286=459 与 286+173=459 是同一种组合!
#!usr/bin/env python
# -*- coding: utf-8 -*-def is_same(num_1, num_2, freq):''' Judge the elements of two numbers whether they are same or not!Different method of find same element ---->ahalei_15'''for i in range(freq):for j in range(freq):if str(num_1)[i] == str(num_2)[j]:return Trueelif i != j and str(num_1)[i] == str(num_1)[j]:return Trueelif i != j and str(num_2)[i] == str(num_2)[j]:return True
def has_zero(num):''' Judge var_number whether it has 0 or not!'''for k in str(num):if k == str(0):return True #else <------Be careful!size = 0 #init size
for a in range(100, 1000):for b in range(100, 1000):if is_same(a, b, 3) is True or has_zero(a) is True or has_zero(b) is True:continueelse:for c in range(100, 1000):if is_same(a, c, 3) is True or is_same(b, c, 3) is True or has_zero(c) is True:continueelif a+b == c:size += 1print('{0}={1}+{2}...............size is {3}'.format(c, a, b, size))
print(size/2)
这篇关于ahalei_14的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!