该函数的剂量如何:当n = 4时; [英] How dose this function works: when n=4;

查看:101
本文介绍了该函数的剂量如何:当n = 4时;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

static void what(int[] x, int n)
{

    if (n == 0)
    {
        for (int i = x.Length - 1; i >= 0; i--)

            Console.Write("{0}", x[i]);
        Console.WriteLine();
    }
    else
    {
        x[n - 1] = 0;
        what(x, n - 1);
        x[n - 1] = 1;
        what(x, n - 1);
    }
}





我尝试了什么:



输出为



What I have tried:

the output are

0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111



i只想知道我们如何得到它们


i just wanna know how we got them

推荐答案

参见这些文章:

[ dotnetperls Recursion ]

[ Recursive-methods-in-Csharp ]
See these articles:
[dotnetperls Recursion]
[Recursive-methods-in-Csharp]


Quote:

这个函数是如何工作的:当n = 4时;

How does this function works: when n=4;

它是递归的,该函数调用自身。



有一个工具可以让你看到你的代码在做什么,它的名字是调试器。掌握调试器不是可选的,它对任何程序员都是强制性的,也不例外。

它也是一个很好的学习工具,因为它向你展示了现实,你可以看到哪个期望与现实相符。

当你不明白你的代码在做什么或为什么它做它的作用时,答案是调试器

使用调试器查看你的代码是什么代码正在做。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量,这是一个令人难以置信的学习工具。



调试器 - 维基百科,免费的百科全书 [ ^ ]

在Visual Studio中调试C#代码 - YouTube [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

调试器中没有魔法,它没有找到错误,它只是帮助你。如果代码没有达到预期的效果,那么你就接近了一个bug。

It is recursive, the function calls itself.

There is a tool that allow you to see what your code is doing, its name is debugger. Mastering the debugger is not optional, it is mandatory for any programmer, no exception.
It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Debugging C# Code in Visual Studio - YouTube[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


你可以把它想象成一系列的回调。您的方法进行2次递归调用。反过来,这些调用会进行2次递归调用。因此,您可以将其可视化为方法whatA调用方法whatAA和whatAB。然后方法WhatAA调用方法whatAAA和whatAAB等。做这样的事情

You can visualise this as a series of callbacks. Your method makes 2 recursive calls. These calls, in turn, make 2 recursive calls. So you can visualise it as method whatA calling methods whatAA and whatAB. Then Method WhatAA calling method whatAAA and whatAAB etc. Do something like this

whatA(2)
  n=2, arr[1]=0
  what AA(1)        //stack push A
   {
    n=1, arr[0]=0
      what AAA(0)    //stack push AA  
      {
        n=0, prints 0,0
        returns
      }              //stack pop  AA
    n=1,arr[0]=1 
      what AAB(0)    // stack push AA
      {
        n=0, prints 0,1
        returns
      }              //stack pop AA
    }                //stack pop A
    n=2, arr[1]=1  
  what AB(1)         //stack push A
   {
     n=1, arr[0]=1
       what ABA(0)   //stack push AB
       {
        prints 1,1
        returns
       }             //stack pop AB
   }                 //stack pop A


这篇关于该函数的剂量如何:当n = 4时;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆