這篇文章主要介紹了C語言實現的階乘,排列和組合的方法,涉及C語言數學運算的相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了C語言實現的階乘,排列和組合。分享給大家供大家參考。具體如下:
- #include<stdio.h>
- int Factorial(int n)
- {
- int i=0;
- int sum=1;
- int array[]={0};
- for(i=n;i>=1;i--)
- {
- sum=sum*i;
- }
- return sum;
- }
- int Arrangement(int n,int m)
- {
- int result=0;
- if(m>n)
- {
- printf("您的輸入有錯,上邊的數不能比下邊大! OK?");
- return 0;
- }
- if(m<n)
- {
- result=Factorial(n)/Factorial(n-m);
- return result;
- }
- }
- int Combination(int n,int m)
- {
- int result=0;
- if(m>n)
- {
- printf("您的輸入有錯,上邊的數不能比下邊大! OK?");
- return 0;
- }
- if(m<n)
- {
- int temp=0;
- temp=Factorial(n-m)*Factorial(m);
- result=Factorial(n)/temp;
- return result;
- }
- }
- main()
- {
- int res=0;
- res=Arrangement(5,2);
- printf("%d",res);
- system("pause");
- }
希望本文所述對大家的C語言程序設計有所幫助。
新聞熱點
疑難解答