This article is a mirror article of machine translation, please click here to jump to the original article.

View: 18710|Reply: 0

[Source] Interview algorithm questions 1, 1, 2, 3, 5, 8, 13, 21, 34...... find out what the 30th digit is, and use a recursive algorithm to find out

[Copy link]
Posted on 9/26/2015 10:28:04 AM | | |

As you can see from the above, the first two numbers are added to get the third number.

public static int SuanFa (int i)
        {
             if(i>0)
              {
                  if (1 >= i || i <= 2)

                      return 1;

                   if(i>2)

                      return SuanFa(i-1) + SuanFa(i-2);

              }
              else

                 return 0;

        }



SuanFa (i-1) represents the number before it was calculated.

SuanFa (i-2) represents the first second number from which it is calculated.

or

public int GetNumberAtPos(int pos)

{
        if(pos==0|| pos==1)

       {
             return 1;
       }
        int res = GetNumberAtPos(pos - 1) + GetNumberAtPos(pos - 2);
        return res;
  }

or

public int show(int i){
        
//         Console.WriteLine(show(30));
        if(i<=0){
         a=0;
        }
        if(i==1&&i<=2){
            a=1;
            
        }
        else{
            a=show(i-1)+show(i-2);
        }
        return a;
        
    }






Previous:The Next Generation of Cloud Computing Models: Docker is revolutionizing personalized commerce
Next:The type of interface or method that can be traversed with foreach needs to be implemented.
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com