• n的阶乘


    #include <iostream>
    using namespace std;

    // n的阶乘 -- 1. 递归方法(必须要是返回值函数,而不能是void类型函数)
    int factorial1(int n)
    {
      if (n <= 0)
        printf("值为负数,不能判断!!! ");
      else if (n==1)
        return 1;
      else
        return n * factorial1(n - 1);
    }

    // n的阶乘 -- 2. 循环方法
    int factorial2(int n)
    {
      int mul=1;
      while (true) {
        mul *= n;
        n--;
        if (n == 1)
          break;
      }
      return mul;
    }

    int main()
    {
      int n;
      cin >> n;
      printf("递归:数 %d 的阶乘是 %d ", n,factorial1(n));
      printf("循环:数 %d 的阶乘是 %d ", n, factorial2(n));;
      return 1;
    }

    在本博客上的内容全部共享公开不收费 转载请注明出处,尊重知识,尊重劳动 对代码或者知识有疑问,可联系博主(qq):2218787597(或邮件投递)
  • 相关阅读:
    模块jieba库的使用
    模块wordcloud库的使用
    爬取哔哩哔哩网站弹幕
    爬虫讲解
    基本统计值计算
    数据类型及元素元组
    pyinstall的安装及使用
    面向对象介绍
    re模块
    logging模块
  • 原文地址:https://www.cnblogs.com/TyranRex/p/12162184.html
Copyright © 2020-2023  润新知