Visual Basic
Module Module1 Function pro(ByVal x As Integer) As Integer Dim i, y As Integer y = 1 For i = 1 To x y = y * i Next pro = y End Function Sub Main() Dim m, n, a As Integer m = Console.ReadLine() n = Console.ReadLine() a = pro(m) / (pro(m - n) * pro(n)) Console.Write(a) Console.ReadKey() End Sub End Module
C language
#include<stdio.h>
int function(int x);
void main(void)
{
int c,x,y,z;
printf("Enter m:\n");
scanf("%d", &x);
printf("Enter m:\n");
scanf("%d", &y);
z=function(x-y);
x=function(x);
y=function(y);
c = x / (y * z);
printf("C=%d",c);
getch();
}
int function(int x)
{
int i,p;
p=1;
for(i=1;i<=x;i++)
{
p=i*p;
}
return p;
}
本程式為m個相異物品中,任意不重複取出n個,不計前後次序的組合方法。即C(m取n)=m!/(m-n)!*n!。
全站熱搜