Python编程问题求解

altman87120 2010-09-25 08:35:17
求解各位大哥:
1到22, 22个数字分成3组,前两组有8个数,后一组有6个数字,请问这样的组合一共有多少组呢?并将其列出来。谢谢各位大大!
...全文
233 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
feilniu 2010-09-28
  • 打赏
  • 举报
回复

>>> import math
>>> f = math.factorial
>>> def C(n, k):
return f(n) // (f(k) * f(n - k))

>>> C(22,8) * C(14,8) * C(6,6) // 2
480134655

zhuanqingshan 2010-09-28
  • 打赏
  • 举报
回复
>>>
960269310
zhuanqingshan 2010-09-28
  • 打赏
  • 举报
回复

def P(n, m):
assert(n>=m)
if m == 0: return 1
return reduce(lambda x,y:x*y, range(n-m+1,n+1))

def factorial(n):
assert(n >= 0 )
if n == 0: return 1
return reduce(lambda x,y:x*y, range(1, n+1))


def C(n, m):
return P(n, m)/factorial(m)

print C(22, 8) * C(14, 8)
feilniu 2010-09-27
  • 打赏
  • 举报
回复
1到22, 22个数字分成3组,前两组有8个数,后一组有6个数字

这样的组合 = C(22,6) * C(16,8) * C(8,8) / 2 = C(22,8) * C(14,8) * C(6,6) / 2 = 480134655
notax 2010-09-27
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 javacode007 的回复:]

应该是C(22,8)*C(14,8)*C(6,6)这么多吧
其中C(n,m)表示从 n 个数据中选取 m 个进行组合。
[/Quote]

+1
李察德-泰森 2010-09-27
  • 打赏
  • 举报
回复
既然是组合,那就是没顺序的了

#include <iostream>

using namespace std;

int main()
{
int res_1 = 0;
int res_2 = 0;
int res_3 = 0;
for(int i = 15; i <= 22; i++)
res_1 += i;
for(int i = 7; i <= 14; i++)
res_2 += i;
for(int i = 1; i <= 6; i++)
res_3 += i;

cout << "res_1 = " << res_1 << endl;
cout << "res_2 = " << res_2 << endl;
cout << "res_3 = " << res_3 << endl;
cout << "result = " << res_1*res_2*res_3 << endl;

return 0;
}
I_NBFA 2010-09-26
  • 打赏
  • 举报
回复
直觉上好像是C(C(22, 16) * 2, 2), 还没思路彻底证明。
I_NBFA 2010-09-26
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 javacode007 的回复:]
应该是C(22,8)*C(14,8)*C(6,6)这么多吧
其中C(n,m)表示从 n 个数据中选取 m 个进行组合。
[/Quote]
假设8个数分3组,12组长度为3,3组长度2,你这样12组会出现重复:
123 456 78
456 123 78
我感觉应该合并12组当成长度为16的组合算C(22, 16).
而且C(6, 6)第3组只有1种组合显然是不对的。
123 456 78
723 856 14
.
.
.
javacode007 2010-09-26
  • 打赏
  • 举报
回复
应该是C(22,8)*C(14,8)*C(6,6)这么多吧
其中C(n,m)表示从 n 个数据中选取 m 个进行组合。
zealand_1 2010-09-26
  • 打赏
  • 举报
回复
应该是4115439900多组吧,太多了。呵呵
iambic 2010-09-25
  • 打赏
  • 举报
回复
这是个数学问题,不要编程求解。

37,743

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧