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# - Fatal编程技术网

如何在C#中使其成为递归语句?

如何在C#中使其成为递归语句?,c#,C#,程序需要在一个表中求出2的幂,该表显示n=1到10时的n和2n。这是我的节目: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Power { class Program { static void Main(string[] args) {

程序需要在一个表中求出2的幂,该表显示n=1到10时的n和2n。这是我的节目:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Power
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine($"Number       Power of 2");
            Console.WriteLine("------------------------");


            for (long counter = 1; counter <= 10; ++counter)
            {
                Console.WriteLine($"{counter, 2} {Power(counter), 20}");
            }

            Console.ReadLine();
        }

        static long Power(long number, long exponentValue = 2)
        {
            long result = 1;
            for (int i = 1; i <= number; i++)
            {
                result *= exponentValue;
            }
                return result;

        }


    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
名称空间幂
{
班级计划
{
静态void Main(字符串[]参数)
{
Console.WriteLine($“2的数字幂”);
Console.WriteLine(“---------------------------”);
对于(长计数器=1;计数器[RANT]
这是一个人工问题,旨在教你递归,但做得不好,因为有更好的非递归解决方案(如你当前的解决方案)。事实上,你通常希望将递归解决方案转换为循环,以避免可能的堆栈溢出错误!
[/RANT]

一般来说:要将循环转换为递归解决方案,您需要找到一种方法来计算解决方案的一部分,然后再次调用该方法来计算解决方案的其余部分,直到完全解决为止。还必须有一种方法来检测何时不需要进一步的递归

在计算幂的情况下,答案是:

Pow(X,N) => N * Pow(X,N-1)
以及:

因此,考虑一下这对于X=2和N=3是如何工作的:

Pow(2,3) => 2 * Pow(2,2)
Pow(2,2) => 2 * Pow(2,1)
Pow(2,1) => 2 (the terminating condition)
这里的结果是2*2*2=8,实际上是2^3

请注意,N值是如何每次下降1,直到达到1为止,此时不再需要递归

用C#术语书写:

注意:我没有编写任何错误处理来关注递归部分。实际代码将检查exponentValue是否大于等于1,依此类推


进一步说明:我使用了这个术语。你的理解是错误的,所以我建议你纠正这个错误!指数是数字自身相乘的次数。

此代码如下所示:

static long Power(long number, long exponentValue = 2)
{   
    if (number < 1)
    {
        return 0; // or throw exception because this shouldn't happen
    }

    if (number == 1)
    {
        return exponentValue;
    }

    return exponentValue * Power(number -  1, exponentValue);
}
静态长功率(长数值,长指数值=2)
{   
如果(数字<1)
{
返回0;//或引发异常,因为这不应发生
}
如果(数字==1)
{
返回指数值;
}
返回指数值*幂(数字-1,指数值);
}

While for Java(和另一种算法),非常类似于您所寻找的。通过调用自身直到满足条件。并且您没有计算幂,不是吗?编辑:无需担心,只是奇怪的namingAh,我将坚持正确的命名。我将更新我的答案以注意这一点。我认为最好使用正确的命名并告诉OP
static long Power(long number, long exponentValue)
{
    if (exponentValue == 1)
        return number;
    else
        return number * Power(number, exponentValue - 1);
}
static long Power(long number, long exponentValue = 2)
{   
    if (number < 1)
    {
        return 0; // or throw exception because this shouldn't happen
    }

    if (number == 1)
    {
        return exponentValue;
    }

    return exponentValue * Power(number -  1, exponentValue);
}
static long Power(long number, long exponentValue = 2)
{
    if (number == 1)
    { 
        return 1;
    }
    else
    { 
        return Power(number - 1, exponentValue) * exponentValue;
    }
}