Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.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)_C#_C++_Loops_Recursion_Factorial - Fatal编程技术网

C# 递归循环(C)

C# 递归循环(C),c#,c++,loops,recursion,factorial,C#,C++,Loops,Recursion,Factorial,谁能给我解释一下吗?我编写了一个函数来计算C中的数字的阶乘: public int factorial(int input) { if (input == 0 || input == 1) return 1; else { int temp = 1; for (int i = 1; i <= input; i++) temp = temp * i; return temp; } } 但是我发现一些C++代码我不知道任

谁能给我解释一下吗?我编写了一个函数来计算C中的数字的阶乘:

public int factorial(int input)
{
    if (input == 0 || input == 1)
        return 1;
else
{
    int temp = 1;
    for (int i = 1; i <= input; i++)
        temp = temp * i;
    return temp;
    }
}
<>但是我发现一些C++代码我不知道任何C++ BTW,它使用递归循环找到阶乘:

int factorial(int number) {
 int temp;
 if(number <= 1) return 1;
 temp = number * factorial(number - 1);
 return temp;
}

有人能给我解释一下它是怎么工作的吗?谢谢。

好吧,它使用了factorialn是n*factorialn-1的事实,基本情况是n=1

例如:

factorial(5) = 5 * factorial(4)
             = 5 * 4 * factorial(3)
             = 5 * 4 * 3 * factorial(2)
             = 5 * 4 * 3 * 2 * factorial(1)
             = 5 * 4 * 3 * 2 * 1

实现仅使用此递归定义。

让我们逐行分析此定义:

if(number <= 1) return 1;
temp = number * factorial(number - 1);
return temp;
所以整个事情都扩大了。它只是使用了

n!=n*n-1*…*2*1=n*n-1


警告:递归代码与迭代或尾部递归优化的版本相比,会受到堆栈溢出和内存使用增加的影响,所以请自行使用。

语法上,C++代码与C语言编写的代码相同。不要让语言差异引起你的警惕!在我看来,它实际上很像C,因为变量是在函数的顶部声明的;在C++或C中,这不是严格必要的。我更喜欢在第一次使用变量时声明变量,将声明和初始化结合在一个语句中,但这只是一个风格上的偏好,不会改变代码的功能。 我将尝试通过在代码段的每一行添加注释来解释这一点:

// Declare a function named "Factorial" that accepts a single integer parameter,
// and returns an integer value.
int Factorial(int number)
{
    // Declare a temporary variable of type integer
    int temp;

    // This is a guard clause that returns from the function immediately
    // if the value of the argument is less than or equal to 1.
    // In that case, it simply returns a value of 1.
    // (This is important to prevent the function from recursively calling itself
    // forever, producing an infinite loop!)
    if(number <= 1) return 1;

    // Set the value of the temp variable equal to the value of the argument
    // multiplied by a recursive call to the Factorial function
    temp = number * Factorial(number - 1);

    // Return the value of the temporary variable
   return temp;
}

理解代码如何工作的一个好方法是将其添加到测试项目中,然后使用调试器单步执行代码。Visual Studio在C应用程序中对此有非常丰富的支持。您可以观察函数如何递归地调用自身,观察每一行按顺序执行,甚至可以看到变量的值随着对它们执行的操作而变化。

递归函数是在其主体中调用自身的函数。要使其有界并最终返回值,必须发生两件事:

它必须有一个基本情况,即它不会再次调用自己,返回一个已知值。此基本情况停止递归。对于阶乘函数,当输入为0时,该值为1

它的递归应用程序必须收敛到基本情况。对于阶乘,递归应用程序调用函数时,输入值减去1,最终会收敛到基本情况

查看此函数的更简单方法是,此函数仅对正输入有效:

int factorial(int input) {
  return input == 0 ? 1 : input * factorial(input - 1);
}

递归函数是从同一个函数调用的函数

例如:

看看代码,它会一次又一次地调用函数

递归的问题它将无限工作,所以你想通过一个特定的条件来停止它吗

上面代码中的一些更改现在看起来

int i=0; 
Test()
        {
         if(i==10) return;
         i++;
         Test();
         cout<<i;
        }

输出将被打印10次,这一行可以将代码复制到你的C程序中,它应该可以工作。基本上它是一个递归函数/方法。它是如何工作的?试着用手模拟它,例如,对于数字=5,你会很容易理解它;LLS:问题是,我不明白递归函数是什么主要部分是C++代码中的第四行,这是混淆的。迪杰马尔:我试过了,但我还是被第四行搞糊涂了从逻辑上而不是编程上考虑它。它说,取前面的阶乘,它是同一个函数并不重要,它只是碰巧是,然后乘以n。@david:我明白了。我只是想告诉你,你根本不需要知道任何关于C++的知识。我相信其他人会给你很好的解释。从数学上讲,n=0应该是基本情况,n=1应该取决于0!=这里有这么多精彩的回答,但你的回答很好。非常感谢也是的,我会摆弄调试器,看看它是如何工作的。
int factorial(int input) {
  return input == 0 ? 1 : input * factorial(input - 1);
}
  Test()
    {
      i++;
      Test();
      cout<<i;
    }
int i=0; 
Test()
        {
         if(i==10) return;
         i++;
         Test();
         cout<<i;
        }