小学三年级数学题!用数字 1 到 8 组成两个三位数使其和为 1000

2017-10-07 02:01:43 +08:00
 crazybug
#用数字 1 到 8 组成两个三位数使其和为 1000,这两个三位数里面的数字不能重复。
#问能写几组?
#用小学三年级的解题思路还真没想到。
#只好用笨方法跑个小程序了。

L=list(range(1,9))
#print(L)

s=""
S=[]
for l in L:
    for l1 in L:
    	for l2 in L:
    		if str(l)<>str(l1) and str(l)<>str(l2) and str(l1)<>str(l2):
    		    s=str(l) + str(l1) + str(l2)
    		    S.append(s)


def IsSame(a,b):
	v=True
	for al in a:
		for bl in b:
			if al==bl:
				v= False
	return v


C1000=[]
for c in S:
	v=1000-int(c)
	if str(v)[0]<>str(v)[1] and str(v)[0]<>str(v)[2] and str(v)[1]<>str(v)[2] and str(v)[2]<>'9' and IsSame(c,str(v)):
	    C1000.append(c)

#print(C1000)
#print(len(C1000))

n=1
DisplayList=[]
for i in range(len(C1000)/2):
	DisplayList.append(C1000[i] + " + " + C1000[len(C1000)-n] + " = 1000")
	n+=1

print(DisplayList)

最终结果有 24 组:

'124 + 876 = 1000', '126 + 874 = 1000', 
'143 + 857 = 1000', '147 + 853 = 1000', 
'153 + 847 = 1000', '157 + 843 = 1000', 
'174 + 826 = 1000', '176 + 824 = 1000', 
'214 + 786 = 1000', '216 + 784 = 1000', 
'284 + 716 = 1000', '286 + 714 = 1000',
'342 + 658 = 1000', '348 + 652 = 1000', 
'352 + 648 = 1000', '358 + 642 = 1000', 
'413 + 587 = 1000', '417 + 583 = 1000',
'432 + 568 = 1000', '438 + 562 = 1000', 
'462 + 538 = 1000', '468 + 532 = 1000', 
'483 + 517 = 1000', '487 + 513 = 1000'
7912 次点击
所在节点    分享发现
25 条回复
larsenlouis
2017-10-07 11:51:06 +08:00
更短的笨方法

```
from itertools import combinations, permutations

pick_6_numbers = combinations(range(1,8+1), 6)
index_mappings = list(permutations(range(6), 6))
num1_set = set()
count = 0
for picked in pick_6_numbers:
for a,b,c,d,e,f in index_mappings:
num1 = 100 * picked[a] + 10 * picked[b] + picked[c]
num2 = 100 * picked[d] + 10 * picked[e] + picked[f]
if num1 + num2 == 1000:
num1_set.add(num1)
if num2 not in num1_set:
count += 1
print('{} + {} = 1000'.format(num1, num2))
print('total: {}'.format(count))
```
liuminghao233
2017-10-07 13:24:27 +08:00
卧槽写出其中一组还算正常

问能写几组就有点过分了
sunine
2017-10-07 22:24:23 +08:00
@crazybug 讲的时候把千位数百位数改成百位数十位数,之前写得太快没注意[捂脸]
Telegram
2017-10-08 00:07:31 +08:00
@liuminghao233 #22 对,现在小学生的题目感觉都是大题。
sevenknights
2017-10-09 10:22:59 +08:00
/*有推理的功夫直接穷举得了*/
main(a, c, s){
for(char r[32]; sprintf(r, "%d + %d", a, 1000 - a) && a++ < 500;)
for(s = 0, c = '1'; c < '9'; ++c)
if((s += !!strchr(r, c)) == 6)
printf("%s = 1000 \n", r);
}

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/395719

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX