31
1
using System;
2
3
public class Program
4
{
5
public static void Main()
6
{
7
Console.WriteLine("Chương trình tính tổ hợp C(n,r)");
8
int n,r;
9
Console.WriteLine("Nhập vào n:");
10
n = int.Parse(Console.ReadLine());
11
Console.WriteLine("Nhập vào r:");
12
r = int.Parse(Console.ReadLine());
13
if(n < r)
14
Console.WriteLine("Không tồn tại tổ hơp C({0},{1})",n,r);
15
else
16
Console.WriteLine("Kết quả tính tổ hợp C({0},{1}) là {2}",n,r,ToHop(n,r));
17
}
18
//Chương trình tính tổ hợp
19
public static long ToHop(int n, int r)
20
{
21
return GiaiThua(n)/(GiaiThua(r)*GiaiThua(n-r));
22
}
23
//Chương trình con tính giai thừa
24
public static long GiaiThua(int n)
25
{
26
long kq = 1;
27
for(int c=1; c<=n; c++)
28
kq = kq * c;
29
return kq;
30
}
31
}
Cached Result