登录|立即注册|使用QQ帐号登录
论坛 > 程序设计/上位机
发帖|
看2245|回0|收藏
楼主 ID:71259 只看他
2014-12-30 02:01
本帖最后由 daming 于 2014-12-30 02:16 编辑

代码:

  1. #include<iostream>
  2. using namespace std;
  3. void main()
  4. {
  5. int fc(int);
  6. cout<<"please input data:\n";
  7. int n;
  8. cin>>n;
  9. cout<<n<<"的阶乘是"<<fc(n)<<endl;
  10. }
  11. int fc(int n)
  12. {
  13. if(n==1||n==0)
  14.   return 1;
  15. else
  16.   return n*fc(n-1);
  17. }



  18. *************************************

  19. #include<iostream>
  20. using namespace std;

  21. void main()
  22. {
  23. int comm(int,int );
  24. int n,k;

  25. cout<<"从n个人中选k个人的不停组合数。"<<endl;
  26. cout<<"n:";  cin>>n;
  27. cout<<"k:";  cin>>k;
  28. cout<<"共有"<<comm(n,k)<<"种组合数。\n";
  29. }

  30. int comm(int n,int k)                                      // 一般的递归函数不过就是一条if—else语句,一条语句写结束条件,
  31. {                                                          // 另一条写递推方程
  32. if(k==0||k==n)
  33.   return 1;
  34. else
  35.   return comm(n-1,k)+comm(n-1,k-1);
  36. }


51黑电子论坛

Powered by Discuz! X3.1

首页|标准版|触屏版|电脑版