什么是 C# 中的递归方法调用?
C#中的递归方法调用称为递归。让我们看一个使用递归计算数字幂的示例。
在这里,如果幂不等于0,则发生函数调用,最终是递归-
if (p!=0) {
return (n * power(n, p - 1));
}上面,n是数字本身,每次迭代的功率都会降低,如下所示-
示例
using System;
using System.IO;
public class Demo {
public static void Main(string[] args) {
int n = 5;
int p = 2;
long res;
res = power(n, p);
Console.WriteLine(res);
}
static long power (int n, int p) {
if (p!=0) {
return (n * power(n, p - 1));
}
return 1;
}
}
作者头像
作者名称
作者性别



评论列表