网络上的:
Code
public int GetNumberValue(int pos)
{
if (pos == 0 || pos == 1)
{
return 1;
}
if (pos < 0 || pos <= 2) //注:递归其实真正是从2开始的,所以要第2位时输出的还是1
{
return 1;
}
else
{
int res = GetNumberValue(pos - 1) + GetNumberValue(pos - 2); //递归求值
return res;
}
} 自己的:
Code
int iResult = 0;
/// <param name="iFirst">首位</param>
/// <param name="iSecond">次位</param>
/// <param name="iDigit">位数</param>
/// <param name="iMaxDigit">求第几位</param>
/// <returns></returns>
private int GetNumber(int iFirst, int iSecond, int iDigit,int iMaxDigit)
{
if (iDigit <= 2)
{
iResult = 1;
return GetNumber(1, 1, iDigit + 1, iMaxDigit);
}
iResult = iFirst + iSecond;
iDigit++;
if (iDigit <= iMaxDigit)
{ return GetNumber(iSecond, iResult, iDigit, iMaxDigit); }
return iResult;
}
//调用,求第30位
GetNumber(1,1,1,30);
posted @
2009-10-13 17:50
Mars.Chen
阅读(
484)
评论()
编辑
收藏
举报