Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/124.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 阶乘递归_C++_Recursion_Factorial - Fatal编程技术网

C++ 阶乘递归

C++ 阶乘递归,c++,recursion,factorial,C++,Recursion,Factorial,我正试图写一个算法,用递归函数计算一个数的阶乘 这是我的代码: #include <stdio.h> #include <conio.h> #include <iostream> using namespace std; int factorial(int n) { cin >> n; if (n == 1) return 1; return n*factorial(n - 1); } int main()

我正试图写一个算法,用递归函数计算一个数的阶乘

这是我的代码:

#include <stdio.h>
#include <conio.h>
#include <iostream>
using namespace std;
int factorial(int n) {
    cin >> n;
    if (n == 1)
        return 1;
    return n*factorial(n - 1);
}
int main() {
    int n = 0;
    cout<<"Enter a number:";
    cout << factorial(n);
    return 0;
}
#包括
#包括
#包括
使用名称空间std;
整数阶乘(整数n){
cin>>n;
如果(n==1)
返回1;
返回n*阶乘(n-1);
}
int main(){
int n=0;

cout在
factorial
函数中,您正在等待另一个不需要的输入。从
factorial
方法中删除此
cin>>n;

问题中未提出的其他几点:

    阶乘增长非常快,通过使用<代码> INT/C> >您将很快得到溢出。您可以考虑使用64位<代码>长Lo/<代码>。
  • conio.h
    不是标准,应避免使用
  • 在全局范围内使用命名空间std调用
    是非常糟糕的

factorial
函数中,您正在等待另一个不需要的输入。从
factorial
方法中删除此
cin>>n;

问题中未提出的其他几点:

    阶乘增长非常快,通过使用<代码> INT/C> >您将很快得到溢出。您可以考虑使用64位<代码>长Lo/<代码>。
  • conio.h
    不是标准,应避免使用
  • 在全局范围内使用命名空间std调用
    是非常糟糕的

    • 您的程序并不是什么都不做。它正在做您不期望的事情。cin>>n应该在主函数中,而不是在阶乘函数中


      它实际上是将每个新数字作为对阶乘的函数调用放在堆栈上。每次重新输入时,您都在更改阶乘的项。例如,您输入“4”,然后输入“6”,然后输入“8”…堆栈上的是顶部的阶乘(8),然后是阶乘(6),然后是阶乘(4)。最终您必须输入“1”这样程序将结束。

      您的程序不是什么都不做。它正在做您不期望的事情。cin>>n应该在主函数中,而不是在阶乘函数中


      它实际上是将每个新数字作为对阶乘的函数调用放在堆栈上。每次重新输入时,您都在更改阶乘的项。例如,您输入“4”,然后输入“6”,然后输入“8”…堆栈上的是顶部的阶乘(8),然后是阶乘(6),然后是阶乘(4)。最终您必须输入“1“以便您的程序结束。

      不要在阶乘方法中要求用户输入。请改为这样做

      int factorial(int n) {
          if (n == 0 || n == 1)
              return 1;
          return n*factorial(n - 1);
      }
      int main() {
          int n;
          cout<<"Enter a number:";
          cin >> n;
          cout << factorial(n);
          return 0;
      }
      
      int阶乘(int n){
      如果(n==0 | | n==1)
      返回1;
      返回n*阶乘(n-1);
      }
      int main(){
      int n;
      cout n;
      
      cout不要在factorial方法中请求用户输入。改为这样做

      int factorial(int n) {
          if (n == 0 || n == 1)
              return 1;
          return n*factorial(n - 1);
      }
      int main() {
          int n;
          cout<<"Enter a number:";
          cin >> n;
          cout << factorial(n);
          return 0;
      }
      
      int阶乘(int n){
      如果(n==0 | | n==1)
      返回1;
      返回n*阶乘(n-1);
      }
      int main(){
      int n;
      cout n;
      你能初始化吗

      n=0;
      
      它不会在main fn中获取您的输入,并始终使用

      factorial(0);
      
      也可以从事实fn中删除cin>>n,然后执行类似的操作

      int factorial(int n)
      { if (n == 0)
         return 1;
         return n*factorial(n-1);
      }
      
      main()
       {
        cin>>n;
        cout<<factorial(n);
        }
      
      int阶乘(int n)
      {如果(n==0)
      返回1;
      返回n*阶乘(n-1);
      }
      main()
      {
      cin>>n;
      你能初始化吗

      n=0;
      
      它不会在main fn中获取您的输入,并始终使用

      factorial(0);
      
      也可以从事实fn中删除cin>>n,然后执行类似的操作

      int factorial(int n)
      { if (n == 0)
         return 1;
         return n*factorial(n-1);
      }
      
      main()
       {
        cin>>n;
        cout<<factorial(n);
        }
      
      int阶乘(int n)
      {如果(n==0)
      返回1;
      返回n*阶乘(n-1);
      }
      main()
      {
      cin>>n;
      
      cout对于大阶乘,使用支持指数表示法的浮点数来表示0很多的数字

      此外,递归所做的是将在数字阶乘中传递的数字推送到堆栈,然后在函数调用最终返回时返回它们,也就是当它为1时

      为了便于说明,请尝试此程序

      // ConsoleApplication1.cpp : Defines the entry point for the console application.
      //
      
      #include "stdafx.h"
      #include <iostream>
      using std::cout;
      using std::endl;
      using std::cin;
      
      int factorialtimes2(int n) {
          if (n <= 1)
              return 1;
          return n*factorialtimes2(n - 1)*2;
      }
      
      int main()
      {
          cout << factorialtimes2(5) << endl;
          cout << 5 * (4 * 2)*(3 * 2)*(2 * 2)*(1 * 2) << endl;
          return 0;
      }
      
      //ConsoleApplication1.cpp:定义控制台应用程序的入口点。
      //
      #包括“stdafx.h”
      #包括
      使用std::cout;
      使用std::endl;
      使用std::cin;
      整数因子2(整数n){
      
      如果(n对于大阶乘,请使用支持指数表示法的浮点来表示0很多的数字

      此外,递归所做的是将在数字阶乘中传递的数字推送到堆栈,然后在函数调用最终返回时返回它们,也就是当它为1时

      为了便于说明,请尝试此程序

      // ConsoleApplication1.cpp : Defines the entry point for the console application.
      //
      
      #include "stdafx.h"
      #include <iostream>
      using std::cout;
      using std::endl;
      using std::cin;
      
      int factorialtimes2(int n) {
          if (n <= 1)
              return 1;
          return n*factorialtimes2(n - 1)*2;
      }
      
      int main()
      {
          cout << factorialtimes2(5) << endl;
          cout << 5 * (4 * 2)*(3 * 2)*(2 * 2)*(1 * 2) << endl;
          return 0;
      }
      
      //ConsoleApplication1.cpp:定义控制台应用程序的入口点。
      //
      #包括“stdafx.h”
      #包括
      使用std::cout;
      使用std::endl;
      使用std::cin;
      整数因子2(整数n){
      
      如果(n首先,将cin放在main中,而不是实际的递归函数。我修复了它,现在它工作了,谢谢。首先,将cin放在main中而不是实际的递归函数。我修复了它,现在它工作了,谢谢12!是32位有符号或无符号
      int
      中可以容纳的最大阶乘。你能告诉我为什么使用nam调用不好吗全局中的espace std?@marandrei为什么“使用命名空间std”被认为是不好的做法?…64位
      int
      ,你不能走得更远,只能走到20!@WeatherVane是的,但我想在这个阶段使用第三方大数字库对OP.12来说会很困难!是32位有符号或无符号
      int
      中可以容纳的最大阶乘。你能告诉我为什么使用na调用不好吗mespace std在全球范围内?@MariAndrei为什么“使用名称空间std”被认为是不好的做法?…而且您不能再使用64位
      int
      ,只能使用20位!@WeatherVane是的,但我想在现阶段使用第三方大数字库对OP来说会很困难。