欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136

標題: C++語言簡單的遞歸調用 [打印本頁]

作者: daming    時間: 2014-12-30 02:01
標題: C++語言簡單的遞歸調用
本帖最后由 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. }
復制代碼







歡迎光臨 (http://m.raoushi.com/bbs/) Powered by Discuz! X3.1