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.