4位数字的组合,其单个数字总和为5 [英] Combinations of 4-digit numbers whose individual digits sum to 5
问题描述
我正在工作的计分卡上,该列具有代表4种可能结果的列,例如:
I'm working on a scorecard at work which has columns representing 4 possible outcomes, for example:
- 成功,
- 失败,
- 非凡,
- 其他
每个工作人员每月都会根据这些评级进行5次评估.因此, 1个人可能会成功 3个成功, 2个例外, 0个不成功, 0个其他 >.
Each staff member is assessed 5 times in the month against those ratings. So 1 person might have 3 successful, 2 exceptional, 0 unsuccessful, 0 other.
因此,每个结果的最大实例数为5,但实例的总和不能超过5.
So the max instances of each outcome is 5 but the total sum of instances can't be more than 5.
我可以尝试输入所有组合(并弄错)-是否有人知道的功能/公式/VBA会为我列出所有可能的结果组合?
I could try to type out all the combinations (and get them wrong) - is there any function/formula/VBA that anyone knows of that will list out all of the possible combinations of outcomes for me?
e.g. 5000,4100,4010,4001,3200,3020,3002,3110,3011, etc...
推荐答案
由于您的数字范围可以从0005
到5000
,因此您可以编写一个简单的循环来测试每个数字以查看数字是否总计
Since your numbers can range from 0005
to 5000
, you could just write a simple loop that tests each number to see if the digits total 5
:
Sub GetPermutations()
Dim i As Long
For i = 5 To 5000
If SumDigits(i) = 5 Then Debug.Print Format$(i, "0000")
Next
End Sub
Function SumDigits(s As Variant) As Long
Dim i As Long
For i = 1 To Len(s)
SumDigits = SumDigits + CLng(Mid$(s, i, 1))
Next
End Function
或者:
Dim w As Long, x As Long, y As Long, z As Long
For w = 0 To 5
For x = 0 To 5
For y = 0 To 5
For z = 0 To 5
If w + x + y + z = 5 Then Debug.Print w & x & y & z
Next
Next
Next
Next
这篇关于4位数字的组合,其单个数字总和为5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!