|
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;
}
|