C # is implemented by a recursive algorithm: the rules for a number of columns are as follows: 1, 1, 2, 3, 5, 8, 13, 21, 34, how much is the 30th digit _c# tutorial

Source: Internet
Author: User

Method One: Recursive algorithm

<summary>
///The rules for a number of columns are as follows: 1, 1, 2, 3, 5, 8, 13, 21, 34 to find the number of 30th digits, with a recursive algorithm to achieve. (C # language)
</summary>
///<param name= "pos" ></param>
///<returns></returns>
public int Getnumberatpos (int pos)
{
  if (pos==0| | Pos==1)
  {return
    1;
  }
  int res = Getnumberatpos (pos-1) + Getnumberatpos (pos-2);
  return res;
}

Method Two: Do not have recursion

Using System;
Using System.Collections;
Using System.Collections.Generic;

Using System.Text;

    Namespace Test {public class Class1 {private ArrayList list = new ArrayList ();

      Public Class1 () {} public Class1 (int num): base () {int i; for (i = 1; I <= num; i++) {list.
      ADD (calculation (i));
      } private int calculation (int num) {if (num = 1 | | num = 2) return 1;
    else return Convert.ToInt32 (list[num-2]) + Convert.ToInt32 (list[num-3)); public int calculation () {return Convert.ToInt32 (list[list).
    Count-1]);
      The public class Test {public static void Main () {int J;
      int num;
        for (j = 1; J < + j) {Console.WriteLine ("How many digits do you want to count:");
        String Readstr;
        Readstr = Console.ReadLine (); if (!string. IsNullOrEmpty (READSTR)) {if (int).
     TryParse (readstr, out num))     {if (num < 1) continue;
              else {Class1 C1 = new Class1 (num); Console.WriteLine (C1.
            Calculation ());
          } else {continue;
        } else {break;

 }
      }
    }
  }
}

Method Three: Use the loop to realize

public long getnumber (int pos)
{
  long one = 1;
  Long two = 1;
  if (pos = 0 | | | pos = 1)
  {return
    1;
  }
  int i = 3;
  Long sum = 1;
  while (I <= Pos)
  {
    sum = one + two;
    one = two;
    two = sum;
    i++;
  }
  return sum;
}

The above is the entire content of this article, I hope to give you a reference, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.