Day two questions 01

  Again two months later, the last time a blog is July 14, the time or too quickly, then the question is, why so long did not write anything of it? Is it in soy sauce?

  Haha, very ashamed to say it, just started working, going to encounter a variety of problems to solve learning to go, then went to spare time to learn some weird stuff, leading blog has been falling, and in the final analysis, or own laziness, because the mind always feel that today a day of work, from work to relax a bit. Unconscious in this psychological comfort themselves, making their own more and more relaxed, and also because they are a little procrastination, you want to write something had been dragged. . .

  ╮ (¯ ▽ ¯ ") ╭ hey, did not talk much to say, what to see recent source of much always feel the foundation is still solid enough to think of playing amateur basis, working hours and then look to write business logic framework What should be better, much laying the foundation every day things, two questions, bite off more than you can chew, wins the title chosen offer, are interested can see for yourself in the github Oh, link: https: // github.com/CyC2018/CS-Notes

 

The first question: Fibonacci number

  Topic Description : We all know that Fibonacci number, and now asked to enter an integer n, you export item n Fibonacci Fibonacci number sequence. n <= 39

  

  I was thinking: What is the Fibonacci column should still know little formula as shown above; simply means that starting from the third number, and any of a number equal to the previous two numbers, such as 0, 1 , 1,2,3,5,8,13,22 ....... then the simplest idea is to use recursion, look at the code below:

public  class Study01 { 

    public  static  int feibo ( int n-) {
         // have the following two if, then have to remember recursive outlet, then there will be an endless loop 
        IF (n-== 0 ) {
             return 0 ; 
        } 
        IF ( n-== == ||. 1 n-2 ) {
             return . 1 ; 
        } 
        return feibo (. 1-n-) feibo + (2-n- ); 
    } 
    
    public  static  void main (String [] args) { 
        
        int RES = feibo (. 6 ) ; 
        System.out.println (RES); // . 8 

    } 
}

   The above code is written, but the optimization Can it because recursion values ​​will lead to some repetitive calculations such feibo (5) = feibo (4) + feibo (3) = [feibo (3) + feibo (2) ] + feibo (3), where the computer then calculates two feibo (3), where the computer can not calculate and combining like terms like people ah, once calculated feibo (n), the value of n when the significant time, in the recursive calculation process will be a lot of number of such double counting, then we have no way to calculate the number of such repetition to eliminate it? Calculated only once and then save it, then the next recalculation directly get enough;

  

  Improved 1 : Create an array, we start with n = 0, the calculated number of each into an array, then the array number of the n-th position is the result we need

// improved way. 1 
    public  static  int feiboUp01 ( int n-) {
         IF (n-== 0 ) {
             return 0 ; 
        } 
        IF (n-n-||. 1 == == 2 ) {
             return . 1 ; 
        } 
        int [] = ARR new new  int [n-+. 1 ]; 
        ARR [ 0] = 0 ; 
        ARR [ . 1] =. 1 ; 
        ARR [ 2] =. 1 ;
         // this loop will even out each time filling a value into the array, until the calculated arr [n] 
        for ( int I =. 3; I <= n-; I ++) {
            arr[i]=arr[i-1]+arr[i-2];
        }
        return arr[n];
    }
    
    public static void main(String[] args) {
        
        int res = feiboUp01(6);
        System.out.println(res);//8

    }

 

  

  Improved two : do not know if I have found above this approach though clever use of the array, but for us, except for the last number in the array, the other numbers are no necessary, do not compute a very large n , it is necessary a new array it so much? So we can continue to improve it;

// Improvement embodiment 2 
    public  static  int feiboUp02 ( int n-) {
         IF (n-== 0 ) {
             return 0 ; 
        } 
        IF (n-n-||. 1 == == 2 ) {
             return . 1 ; 
        } 
        // three here variables, such as 0,1,1,2,3,5, when the current time is 5, pre is 3, prepre is 2 
        int prepre =. 1 ;
         int pre =. 1 ;
         int current = 0 ;
         // here a little bad appreciated that the number of three pre Current prepre
         // after one cycle, is calculated current = prepre + pre, and at this time also the pre prepre one position to the right, the
        // The original pre directed now current, original prepre directed now pre 
        for ( int I =. 3; I <= n-; I ++ ) { 
            Current = prepre + pre; 
            prepre = pre; 
            pre = Current; 
        } 
        return Current; 
    } 
    public  static  void main (String [] args) { 
        
        int RES = feiboUp02 (. 6 ); 
        System.out.println (RES); 

    }

 

 

 The second question : we can go sideways or vertically to cover a larger rectangle with a small rectangle of 2x1. Will the small rectangle of n 2x1 covers a large rectangle without overlapping 2xn, a total of how many ways?

  This problem is actually still Fibonacci number, but the idea is very interesting, in general we find a way to calculate how many ways the time, could go to the drawing computing go, but here it is a big problem down into sub-problems, and the child the problem can continue to split ...

  If n = 1, only one case,

  If n = 2, only two, or two two are both vertically and sideways

 

   

  If n = 3, then the problem becomes three 2x1 small rectangular non-overlapping 2x3 cover a large rectangle, and the first case, that a filling is put sideways, as shown, the remaining required is equivalent to the filling of n = 2, i.e., the case of 2x2; second case is the first one on end is filled, then again filled a need, then the rest is in the case of n = 1;

             

 

   And so on, when n = 5 when there are two cases, a first case filled with a sideways, that the rest is n = 4; filled two vertically second case, the rest is equivalent to n 3 = kind, the following general formula, not to say, the code words and above is basically the same, not to say. . .

 

 

 

   These two topics is very interesting, can be said to be a topic to write code to let you look at the formula, and the other is actually a topic using a divide and conquer, the so-called divide and conquer, divide and conquer is. The original problem is divided into n smaller size, structure and original problem similar deal with small problems, recursively solve these problems, and then solve the consolidation process.

Guess you like

Origin www.cnblogs.com/wyq1995/p/11530937.html