fork download
  1. using System;
  2.  
  3. class Program
  4. {
  5. static int CalcFactorial(int k)
  6. {
  7. int result = 1;
  8. for (int i = 2; i <= k; i++)
  9. {
  10. result = result * i;
  11. }
  12. return result;
  13. }
  14.  
  15. static int CalcCNonRec(int m, int n)
  16. {
  17. return CalcFactorial(m) / (CalcFactorial(n) * CalcFactorial(m - n));
  18. }
  19.  
  20. static int CalcCRec(int m, int n)
  21. {
  22. if (m <= 1 || n == 0 || n == m)
  23. {
  24. return 1;
  25. }
  26. return CalcCRec(m - 1, n - 1) + CalcCRec(m - 1, n);
  27. }
  28.  
  29. static void Main()
  30. {
  31. Console.WriteLine("Введіть m (від 0 до 12):");
  32. int m = int.Parse(Console.ReadLine());
  33. Console.WriteLine("Введіть n (від 0 до m):");
  34. int n = int.Parse(Console.ReadLine());
  35.  
  36. if (n < 0 || n > m || m > 12)
  37. {
  38. Console.WriteLine("Неправильні дані");
  39. }
  40. else
  41. {
  42. Console.WriteLine("За формулою: " + CalcCNonRec(m, n));
  43. Console.WriteLine("Рекурсивно: " + CalcCRec(m, n));
  44. }
  45. }
  46. }
Success #stdin #stdout 0.02s 26456KB
stdin
   4
   2
stdout
Введіть m (від 0 до 12):
Введіть n (від 0 до m):
За формулою: 6
Рекурсивно: 6