不重复的5个自然数相加得21有几种可能?
问:不重复的5个自然数相加得21有几种可能?
来自浙江省上虞市的网友是你的沈静呀的解答:
使用vbs脚本可以让你获得任意数字的组合的个数。脚本范例如下:
SN = InputBox("请输入一个自然数:", "输入")
N = 0
For I = 0 to 35
For J = I + 1 to 35
For K = J + 1 to 35
For P = K + 1 to 35
For Q = P + 1 to 35
Num = I + J + K + P + Q
If Num = CInt(SN) Then
N = N + 1
Str = Str & I & " + " & J & " + " & K & " + " & P & " + " & Q & " = " & SN & vbCrLf
End If
Next
Next
Next
Next
Next
Str = "五个不相同的自然数的和等于 " & SN & " 共有 " & N & " 个组合:" & vbCrLf & vbCrLf & Str
MsgBox Str, , "组合结果"
将上述代码复制并粘贴到“记事本”,然后保存为 test。
vbs 脚本文件。可自行调整代码中的“0 to 35”的两个起始和终止的自然数。单/双击 test。
vbs 文件,输入一个自然数,如:21,马上得到组合结果,如:
---------------------------
组合结果
---------------------------
五个不相同的自然数的和等于 21 共有 37 个组合:
0 + 1 + 2 + 3 + 15 = 21
0 + 1 + 2 + 4 + 14 = 21
0 + 1 + 2 + 5 + 13 = 21
0 + 1 + 2 + 6 + 12 = 21
0 + 1 + 2 + 7 + 11 = 21
0 + 1 + 2 + 8 + 10 = 21
0 + 1 + 3 + 4 + 13 = 21
0 + 1 + 3 + 5 + 12 = 21
0 + 1 + 3 + 6 + 11 = 21
0 + 1 + 3 + 7 + 10 = 21
0 + 1 + 3 + 8 + 9 = 21
0 + 1 + 4 + 5 + 11 = 21
0 + 1 + 4 + 6 + 10 = 21
0 + 1 + 4 + 7 + 9 = 21
0 + 1 + 5 + 6 + 9 = 21
0 + 1 + 5 + 7 + 8 = 21
0 + 2 + 3 + 4 + 12 = 21
0 + 2 + 3 + 5 + 11 = 21
0 + 2 + 3 + 6 + 10 = 21
0 + 2 + 3 + 7 + 9 = 21
0 + 2 + 4 + 5 + 10 = 21
0 + 2 + 4 + 6 + 9 = 21
0 + 2 + 4 + 7 + 8 = 21
0 + 2 + 5 + 6 + 8 = 21
0 + 3 + 4 + 5 + 9 = 21
0 + 3 + 4 + 6 + 8 = 21
0 + 3 + 5 + 6 + 7 = 21
1 + 2 + 3 + 4 + 11 = 21
1 + 2 + 3 + 5 + 10 = 21
1 + 2 + 3 + 6 + 9 = 21
1 + 2 + 3 + 7 + 8 = 21
1 + 2 + 4 + 5 + 9 = 21
1 + 2 + 4 + 6 + 8 = 21
1 + 2 + 5 + 6 + 7 = 21
1 + 3 + 4 + 5 + 8 = 21
1 + 3 + 4 + 6 + 7 = 21
2 + 3 + 4 + 5 + 7 = 21
---------------------------
确定
---------------------------。