发表评论
#2楼[
楼主]2007-09-05 09:28 |
@Zeus2
有什么解决办法么?
应该要从第三位开始才递归吧
Private Function getPos(ByVal pos As Integer) As Integer
If pos <= 2 Then Return 1
Return getPos(pos - 2) + getPos(pos - 1)
End Function
#4楼[
楼主]2007-09-05 09:53 |
@没剑
我的也是从第三位开始的咧
不用递归不是更好吗?
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]);
}
}
public class test
{
public static void Main()
{
int j;
int num;
for( j=1;j<100;j++)
{
Console.WriteLine("你要计算第多少位:");
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;
}
}
}
}
}
老大真牛
for (i = 1; i <= num; i++)
{
list.Add(Calculation(i));
}
}
return Convert.ToInt32(list[num - 2]) + Convert.ToInt32(list[num - 3]);
#9楼[
楼主]2007-09-05 12:05 |
其实这个方法最大的问题在哪?
在于没有验证pos的正负,这样的一个函数是很容易出错的,也是一个正确的函数,在正确使用的情况下是对的,但特殊情况下就异常了,不知道大家看出来没有
Private Function getPos(ByVal pos As Integer) As Integer
If pos <= 2 Then Return 1
Return getPos(pos - 2) + getPos(pos - 1)
End Function
我的完成没有这个问题
好像有一种用公式来计算的方法,公式好像挺复杂的,好像在网上见过一次。
public int GetNumber(int index) // index从1开始
{
int a1=1,
int a2=1,
int temp;
int count = 3
while(index >= count)
{
temp = a2;
a2 = a1+a2
a1 = temp
count++
}
return a2;
}
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication3
{
class A
{
public int GetNumberAtPos(int pos)
{
if (pos == 0 || pos == 1)
{
return 1;
}
int res = GetNumberAtPos(pos - 1) + GetNumberAtPos(pos - 2);
return res;
}
}
class Program
{
static void Main(string[] args)
{
A n = new A();
Console.WriteLine(n.GetNumberAtPos(29));
Console.ReadLine();
//int i=1;
//while (i < 4)
//{
//}
}
}
}
int a1=1,a2=1,returnValue;
for(int i=2;i<num;i++)
{
returnValue=a2;
a2+=a1;
a1=returnValue;
}
return a1;
public int getNumber(int pos) {
if (pos < 1) {
return -1;
}
if (pos < 3) {
return 1;
}
while (pos > 0) {
return getNumber(pos - 2) + getNumber(pos - 1);
}
return -1;
}
代码效率太差了把
只需要用两个变量保存前两位不就行了 再加一个当前数值,
最后更新一下变量
楼主这个貌似 应该这样
public int GetNumberAtPos(int pos)
{
if (pos == 0 ||pos == -1 )
{
return 0;
}
if ( pos == 1)
{
return 1;
}
int res = GetNumberAtPos(pos - 1) + GetNumberAtPos(pos - 2);
return res;
}
支持13楼的
using System;
using System.Collections.Generic;
using System.Text;
namespace 算法设计_一列数
{
class Program
{
static void Main(string[] args)
{
DateTime time2 = DateTime.Now;
Console.WriteLine(time2.ToString());
int number2 = GetNumber(30);
DateTime time3 = DateTime.Now;
Console.WriteLine(time3.ToString());
TimeSpan t2 = time3.Subtract(time2);
Console.WriteLine(t2.ToString());
Console.WriteLine(number2);
DateTime time = DateTime.Now;
Console.WriteLine(time.ToString());
int number = getNumber(30);
DateTime time1 = DateTime.Now;
Console.WriteLine(time1.ToString());
TimeSpan t1 = time1.Subtract(time);
Console.WriteLine(t1.ToString());
Console.WriteLine(number);
}
public static int getNumber(int pos)
{
if (pos == 0 || pos == 1)
{
return 1;
}
int s = getNumber(pos - 1) + getNumber(pos-2);
return s;
}
public static int GetNumber(int index) // index从1开始
{
int a1=1;
int a2=1;
int temp;
int count = 2;
while(index >= count)
{
temp = a2;
a2 = a1+a2;
a1 = temp;
count++;
}
return a2;
}
}
}
执行以后就可以看出差别了
数值的索引越往后差别越明显!
int fwdadd(int i)
{
int s,m,n,flag;
if(i<3)
return 1;
else
{
m=1;n=1;flag=0;s=1;
for(int k=3;k<=i;k++)
{
flag=1-flag;
if(flag)
m=m+n;
else
n=n+m;
}
if(flag)
return m;
else
return n;
}
}
不用递归也能很简单的
int fibonacci = 0;
for (int i = 0, j = 0, k = 1, fib = 1; i < 30; fib = j + k, j = k, k = fib, i++)
{
fibonacci = fib;
}
Console.WriteLine(fibonacci);
Console.Read();