Recently encountered two interview questions and bondage advertising (rpm)

Poor me, from their homes to the capital, slaughtered every day, work the old did not forthcoming, it is depressing. Forced to make an ad to find a company I enjoyed at home. Of course, I did not intend to throw such advertising on the home page move tiles.

Therefore, the recent share crazy interview two met face questions .

The first topic is very interesting, I met its primary version until today, when the interview to see an upgraded version of the subject, a time I was not sure what the end result is a run. Without further ado, the following topic:
public class A
{
  public static int X;
  static A()
  {
    X = B.Y + 1;
  }
}

public class B
{
  public static int Y = A.X + 1;
  static B()
  {}
}

Console.WriteLine( "X={0},Y={1}", A.X, B.Y );

Asks the output results.

In fact, on the subject of static constructor we have seen before, is to investigate the static constructor and constructor understand the order of execution. This question touches of ingenuity, escalated into two types of static constructors and static member initialization execution order.
We all know that static constructors are part of the static member initialization and static constructor compiled, while static constructor executes when the type is loaded, headache purpose of this question is that, is not very obvious to see in the end is A class loading or front loading type B first. A first time if the class loader is performed static constructor of the Class A, type B is bound to loading, in other words in BY + 1 This expression front result, type B must be loaded, so that Y = 0 (X default zero) + 1 = 1, X = 1 (Y initialized) + 1 = 2. First initialization if class B, the result is the opposite. Finally, I believe that the front loaded type A, the result should be X = 2, Y = 1. But actually did not mind the end.

The second topic today to say very common, in a nutshell is calculated using a recursive algorithm Fibonacci bit N of Number of, I do not know how this topic can be brain-dead brought again and again test programmer , any programmer a basic quality should be able to recognize this problem using an iterative recursion is better than the N times.

Here, I first give solutions that damn recursive algorithm:
int damn Fibonacci recursive algorithm to calculate the function (int index)
{
  return index <= 2 1:?  damn Fibonacci recursive algorithm calculation function (index - 1) +  damn Fibonacci recursive algorithm calculation function (index - 2);
}

Here I have to speak out again, Fibonacci calculated columns much better than the recursive iterative!

Although this topic is brain damage, but little change it, realize that a computing Fibonacci highest computational efficiency of Number function, but it is a very interesting question. I think to really pursue extreme efficiency, we must start from the perspective of IL moment, I can think of such IL The following are the most computationally efficient:

We first assume that there are two local variables int

then:


ldc.i4.1
ldc.i4.1 // push to stack on two 1
ldc.i4.1
1 a stloc.0 // push again, and then put it in a local variable 0.
: Imaginary Label
add // two numbers on the stack pop added and the result onto the stack.
dup // the top of the stack copy of the results
stloc.1 // then the calculated result stored into local variable one .
The ldloc.0 // stack local variable values ​​0
ldloc.1 // the values ​​of local variables onto the stack 1
stloc.0 // the data stack (local variable 1) of the stack and local variables assigned to 0.
Guess what now stacks left? 2 is the result of the operation is a copy, the copy is stored copy to eject a local variable, the value of the local variable 0 is 1, the stack, the stack is 21, the value of the local variable a is 2, and the stack, is on the stack 212, the top of stack 2 is then assigned to the local variable 0 stack, the stack is 21, the local variable 0 is 2, 2 is a local variable.
然后这里跳转到add
br 假想的Label

跳转后,堆栈继续把2+1计算完毕,堆栈上是3,然后2被推进去,堆栈变成3 2,然后两个局部变量的值都被变换为3,又一次循环,堆栈上是5,3被推进去,成为5 3,周而复始计算斐波那契。

接下来的问题是,怎么用C#来体现这个IL逻辑?你能想到的最接近的方案是什么呢?
另外一个问题是,其实这个IL还算不上是效率最高的,应该还有效率更高的算法,你找出来没?

最后容我大吼一声,谁赏口饭吃吧。。。。。

Guess you like

Origin www.cnblogs.com/xyy2019/p/11783357.html