C# 求N的阶乘-递归深度讲解
static void Main()
{
Console.WriteLine("P(n) n equal 0:");
Console.WriteLine("result:{0}", P(0));
Console.WriteLine("P(n) n equal 1:");
Console.WriteLine("result:{0}", P(1));
Console.WriteLine("P(n) n equal 2:");
Console.WriteLine("result:{0}", P(2));
Console.WriteLine("P(n) n equal 3:");
Console.WriteLine("result:{0}", P(3));
Console.WriteLine("P(n) n equal 4:");
Console.WriteLine("result:{0}", P(4));
Console.ReadKey();
}
private static int P(int n)//n=4,3,2,1,0
{
Console.WriteLine("test n values:{0}", n);//4,3,2,1,0,1,2,3,4
if (n == 0)
return 1;
else
return n * P(n - 1);//4*P(3) 3*P(2) 2*P(1) 1*P(0)
}
http://C:\Users\lenovo\Desktop\P.jpg
test result:
P(n) n equal 0:
test n values:0
result:1
P(n) n equal 1:
test n values:1
test n values:0
result:1
P(n) n equal 2:
test n values:2
test n values:1
test n values:0
result:2
P(n) n equal 3:
test n values:3
test n values:2
test n values:1
test n values:0
result:6
P(n) n equal 4:
test n values:4
test n values:3
test n values:2
test n values:1
test n values:0
result:24