C10,5=10!/5!/(10-5)!
!表示阶乘 5!=1*2*3*4*5
三个for循环搞定
main ()
{
int i,iOut,itmp,itmp1,itmp2;
itmp=1;
for (i=1;i<=10;i++) //10的阶乘
itmp=itmp * i;
itmp1=1;
for (i=1;i<=5;i++) //5的阶乘
itmp1=itmp1 * i;
itmp2=1;
for (i=1;i<=10-5;i++) //10-5的阶乘
itmp2=itmp2 * i;
iOut = itmp/itmp1/itmp2; 结果
}
如果你要求组合的数目的话
就是下面这个程序,已经调试过了,是对的
#include
void main()
{
int i,n=1,r=1;
for(i=1;i<11;i++) n*=i;
for(i=1;i<6;i++) r*=i;
printf("组合数:%d",n/(r*r));
}
你的意思是得到任意5个数字的组合么?
#define NUM_CNT 10
#define NUM_SEL 5
int nums [NUM_CNT];
void get_numbers (void)
{
int i;
for (i = 0; i < NUM_CNT; i++)
{
printf("input %dth number:", i+1);
scanf("%d", &nums[i]);
}
}
void select_numbers (void)
{
int i, sel;
char str[12];
char result[1024] = {0};
for (i = 0; i < NUM_SEL; i++)
{
sel = rand()%NUM_CNT;
itoa(nums[sel], str, 10);
strcat(result, str);
}
printf("select %d numbers and the result is %s\n", NUM_SEL, result);
}
void main (void)
{
get_numbers();
select_numbers();
}