求任意數(shù)的階層(遞歸方法)
static int F(int input)
{
? ? ? ? if (input == 1)
? ? ? ? { return 1; }//沒有這兩行是不行的。
? ? ? ? return input * F(input - 1);? ? ? ??
}
static void Main(string[] args)
{
? ? ? ? int n = Convert.ToInt32(Console.ReadLine());
? ? ? ? Console.WriteLine(F(n));? ? ? ?
}
======================================================
普通方法對比:
static int F(int input)
{
? ? ? ??int sum = 1;
? ? ? ? for (int j = 1; j < input + 1; j++)
? ? ? ??{ sum *= j; }
? ? ? ??return sum;
}
static void Main(string[] args)
{
? ? ? ? ?int n = Convert.ToInt32(Console.ReadLine());
? ? ? ? ?int n1 = F(n);
? ? ? ? ?Console.WriteLine(n1);
}
標(biāo)簽: