C語言 求組合數


任務描述

本關任務:編寫一個用函數實現組合數的計算程序。

編程要求

組合數公式為:C(m,k)=m!/(k!(m-k)!),請實現求階乘函數Factorial()和求組合數的函數Combination(),然后從main函數輸入m,k,輸出組合數。

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 int fac(int x);
 4 int main()
 5 {
 6     unsigned int m,k,p;
 7 
 8     printf("Input m,k(10>=m>=k>0):\n");
 9     scanf("%u,%u",&m,&k);
10     p=fac(m)/(fac(m-k)*fac(k));
11     printf("p = %d",p);
12     return 0;
13 }
14 int fac(int x)           //定義階乘函數
15 {
16     int i;
17     long int s = 1;
18     for(i=1;i<=x;i++)
19         s *= i;
20     return s;
21 }

運行結果:Input m,k(10>=m>=k>0):

p = 28


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM