有多少个5位递减数?
问题
A decreasing number is a number where each digit is less than the
digit to its left. For example, 87420 is a decreasing number.
How many five digit decreasing numbers are there?
解
这是一道典型的排列组合问题。首先,由于递减数的定义是高位比低位数字要大,令该数字为abcde,满足a>b>c>d>e,这样我们需要取5个不同的数字。在0–9这10个数字中选取5个不同的数字之后,其从大到小的排列方式是唯一的,因而这个问题就转化成了在10个数字中任意选取5个数字有多少种组合的问题。
我们知道:
(rn)=r!(n−r)!n!由于n=10而r=5,得到:
(510)=5!(10−5)!10!=(5×4×3×2×1)×(5×4×3×2×1)10×9×8×7×6×5×4×3×2×1=5×4×3×2×110×9×8×7×6=2×12×9×2×7×2=2×9×2×7=252因此一共有252个。
写个Python脚本验证一下:
1
2
3
4
5
6
| total = 0
for x in range(43210, 98765 + 1):
a, b, c, d, e = str(x)
if (a > b) and (b > c) and (c > d) and (d > e):
total = total + 1
print(total)
|